結果
問題 |
No.90 品物の並び替え
|
ユーザー |
![]() |
提出日時 | 2014-12-10 00:48:04 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 136 ms / 5,000 ms |
コード長 | 2,668 bytes |
コンパイル時間 | 925 ms |
コンパイル使用メモリ | 107,264 KB |
実行使用メモリ | 22,784 KB |
最終ジャッジ日時 | 2024-06-11 19:07:52 |
合計ジャッジ時間 | 1,956 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 9 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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); } } }