結果

問題 No.357 品物の並び替え (Middle)
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-16 15:50:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 163 ms / 5,000 ms
コード長 1,279 bytes
コンパイル時間 5,658 ms
コンパイル使用メモリ 79,272 KB
実行使用メモリ 57,712 KB
最終ジャッジ日時 2023-10-17 06:54:21
合計ジャッジ時間 6,565 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
57,424 KB
testcase_01 AC 128 ms
57,396 KB
testcase_02 AC 133 ms
57,436 KB
testcase_03 AC 134 ms
57,352 KB
testcase_04 AC 134 ms
57,368 KB
testcase_05 AC 134 ms
57,580 KB
testcase_06 AC 132 ms
57,340 KB
testcase_07 AC 128 ms
57,528 KB
testcase_08 AC 136 ms
57,456 KB
testcase_09 AC 154 ms
57,712 KB
testcase_10 AC 146 ms
57,696 KB
testcase_11 AC 142 ms
57,436 KB
testcase_12 AC 163 ms
57,608 KB
testcase_13 AC 161 ms
57,656 KB
testcase_14 AC 148 ms
57,592 KB
testcase_15 AC 137 ms
57,328 KB
testcase_16 AC 146 ms
57,628 KB
testcase_17 AC 150 ms
57,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        int n = Integer.parseInt(stdin.next());
        int m = Integer.parseInt(stdin.next());
        
        int[][] scores = new int[n][n];
        for (int i = 0; i < m; i++) {
            int item1 = Integer.parseInt(stdin.next());
            int item2 = Integer.parseInt(stdin.next());
            int score = Integer.parseInt(stdin.next());
            scores[item1][item2] = score;
        }
        
        int[] dp = new int[1 << n];
        for (int bit = 0; bit < dp.length; bit++) {
            for (int nxt = 0; nxt < n; nxt++) {
                if ((bit & (1 << nxt)) != 0) {
                    continue;
                }
                
                int score = dp[bit];
                for (int pre = 0; pre < n; pre++) {
                    if ((bit & (1 << pre)) != 0) {
                        score += scores[pre][nxt];
                    }
                }
                
                dp[bit | (1 << nxt)] = Math.max(dp[bit | (1 << nxt)], score);
            }
        }
        
        int ans = Arrays.stream(dp).max().getAsInt();
        System.out.println(ans);
    }
}
0