結果

問題 No.90 品物の並び替え
ユーザー nanophoto12nanophoto12
提出日時 2014-12-10 00:48:04
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 154 ms / 5,000 ms
コード長 2,668 bytes
コンパイル時間 2,144 ms
コンパイル使用メモリ 105,364 KB
実行使用メモリ 26,092 KB
最終ジャッジ日時 2023-09-02 12:26:51
合計ジャッジ時間 3,593 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
21,972 KB
testcase_01 AC 70 ms
26,000 KB
testcase_02 AC 60 ms
22,012 KB
testcase_03 AC 60 ms
21,892 KB
testcase_04 AC 61 ms
24,032 KB
testcase_05 AC 71 ms
25,992 KB
testcase_06 AC 70 ms
25,964 KB
testcase_07 AC 60 ms
21,848 KB
testcase_08 AC 60 ms
21,824 KB
testcase_09 AC 154 ms
26,092 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Yukicoder
{
    public static class Algorithm
    {
        public static bool NextPermutation<T>(T[] array, int start, int length)
            where T : IComparable
        {
            int end = start + length - 1;

            if (end <= start) return false;

            int last = end;
            while (true)
            {
                int pos = last--;

                if (array[last].CompareTo(array[pos]) < 0)
                {
                    int i;
                    for (i = end + 1; array[last].CompareTo(array[--i]) >= 0; ) { }
                    T tmp = array[last]; array[last] = array[i]; array[i] = tmp;
                    Array.Reverse(array, pos, end - pos + 1);
                    return true;
                }
                if (last == start)
                {
                    Array.Reverse(array, start, end - start);
                    return false;
                }
            }

            throw new Exception("NextPermutation: Fatal error");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var first = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var n = first[0];
            var m = first[1];
            int[,] score = new int[n+1,n+1];
            for (int i = 0; i < n + 1; i++)
            {
                for (int j = 0; j < n + 1; j++)
                {
                    score[i, j] = 0;
                }                
            }
            for (int i = 0; i < m; i++)
            {
                var line = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                score[line[0], line[1]] = line[2];
            }
            var permutation = new int[n];
            for (int i = 0; i < n; i++)
            {
                permutation[i] = i;
            }
            var maxScore = 0;
            while (true)
            {
                var sum = 0;
                for (int i = 0; i < permutation.Length - 1; i++)
                {
                    for (int j = i+1; j < permutation.Length; j++)
                    {
                        sum += score[permutation[i], permutation[j]];
                    }                
                }
                maxScore = Math.Max(maxScore, sum);
                if (!Algorithm.NextPermutation(permutation, 0, n))
                {
                    break;
                }
            }
            Console.WriteLine(maxScore);
        }
    }
}
0