結果

問題 No.90 品物の並び替え
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-03-13 22:29:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 862 ms / 5,000 ms
コード長 1,905 bytes
コンパイル時間 2,595 ms
コンパイル使用メモリ 81,036 KB
実行使用メモリ 135,976 KB
最終ジャッジ日時 2023-09-06 13:25:50
合計ジャッジ時間 6,099 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
55,880 KB
testcase_01 AC 268 ms
64,024 KB
testcase_02 AC 129 ms
55,792 KB
testcase_03 AC 198 ms
58,340 KB
testcase_04 AC 194 ms
57,820 KB
testcase_05 AC 265 ms
64,100 KB
testcase_06 AC 281 ms
64,204 KB
testcase_07 AC 156 ms
55,820 KB
testcase_08 AC 132 ms
56,624 KB
testcase_09 AC 862 ms
135,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        
        int n = stdin.nextInt();
        int m = stdin.nextInt();
        
        int[][] score = new int[n][n];
        for (int i = 0; i < m; i++) {
            int a = stdin.nextInt();
            int b = stdin.nextInt();
            int c = stdin.nextInt();
            score[a][b] = c;
        }
        
        List<List<Integer>> permutations = new ArrayList<>();
        Deque<List<Integer>> queue = IntStream.range(0, n)
                                              .mapToObj(i -> Arrays.asList(i))
                                              .collect(Collectors.toCollection(ArrayDeque::new));
        while (!queue.isEmpty()) {
            List<Integer> permutation = queue.pollFirst();
            
            if (permutation.size() == n) {
                permutations.add(permutation);
            } else {
                for (int i = 0; i < n; i++) {
                    if (permutation.contains(i)) continue;
                    List<Integer> nextPermutation = new ArrayList<>(permutation);
                    nextPermutation.add(i);
                    queue.add(nextPermutation);
                }
            }
        }
        
        int ans = 0;
        for (List<Integer> permutation : permutations) {
            int sum = 0;
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    sum += score[permutation.get(i)][permutation.get(j)];
                }
            }
            ans = Math.max(ans, sum);
        }
        
        System.out.println(ans);
    }
}
0