結果

問題 No.357 品物の並び替え (Middle)
ユーザー tentententen
提出日時 2020-12-23 19:16:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 145 ms / 5,000 ms
コード長 1,104 bytes
コンパイル時間 2,510 ms
コンパイル使用メモリ 77,152 KB
実行使用メモリ 57,948 KB
最終ジャッジ日時 2023-10-21 15:15:59
合計ジャッジ時間 4,976 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
57,616 KB
testcase_01 AC 106 ms
57,336 KB
testcase_02 AC 102 ms
56,172 KB
testcase_03 AC 113 ms
57,336 KB
testcase_04 AC 119 ms
57,636 KB
testcase_05 AC 113 ms
57,452 KB
testcase_06 AC 112 ms
57,588 KB
testcase_07 AC 108 ms
57,688 KB
testcase_08 AC 129 ms
57,708 KB
testcase_09 AC 132 ms
57,596 KB
testcase_10 AC 136 ms
57,888 KB
testcase_11 AC 138 ms
57,804 KB
testcase_12 AC 140 ms
57,772 KB
testcase_13 AC 141 ms
57,624 KB
testcase_14 AC 132 ms
57,708 KB
testcase_15 AC 122 ms
57,420 KB
testcase_16 AC 140 ms
57,948 KB
testcase_17 AC 145 ms
57,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static int n;
    static int[][] score;
    static int[] dp;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        int m = sc.nextInt();
        score = new int[n][n];
        for (int i = 0; i < m; i++) {
            score[sc.nextInt()][sc.nextInt()] = sc.nextInt();
        }
        dp = new int[1 << n];
        Arrays.fill(dp, -1);
        dp[0] = 0;
        System.out.println(dfw((1 << n) - 1));
    }
    
    static int dfw(int mask) {
        if (dp[mask] < 0) {
            for (int i = 0; i < n; i++) {
                if ((mask & (1 << i)) == 0) {
                    continue;
                }
                int sum = 0;
                for (int j = 0; j < n; j++) {
                    if (i == j || (mask & (1 << j)) == 0) {
                        continue;
                    }
                    sum += score[j][i];
                }
                dp[mask] = Math.max(dp[mask], dfw(mask ^ (1 << i)) + sum);
            }
        }
        return dp[mask];
    }
}
0