結果

問題 No.357 品物の並び替え (Middle)
ユーザー htensaihtensai
提出日時 2020-01-30 12:50:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 135 ms / 5,000 ms
コード長 1,647 bytes
コンパイル時間 2,459 ms
コンパイル使用メモリ 79,852 KB
実行使用メモリ 55,976 KB
最終ジャッジ日時 2023-10-14 08:23:22
合計ジャッジ時間 5,117 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
49,584 KB
testcase_01 AC 39 ms
49,300 KB
testcase_02 AC 41 ms
49,240 KB
testcase_03 AC 43 ms
49,240 KB
testcase_04 AC 46 ms
49,832 KB
testcase_05 AC 44 ms
49,512 KB
testcase_06 AC 42 ms
49,308 KB
testcase_07 AC 38 ms
49,388 KB
testcase_08 AC 54 ms
50,728 KB
testcase_09 AC 85 ms
55,976 KB
testcase_10 AC 74 ms
52,212 KB
testcase_11 AC 62 ms
50,708 KB
testcase_12 AC 135 ms
55,652 KB
testcase_13 AC 118 ms
55,504 KB
testcase_14 AC 91 ms
55,828 KB
testcase_15 AC 56 ms
51,684 KB
testcase_16 AC 88 ms
53,404 KB
testcase_17 AC 71 ms
51,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    static HashMap<Integer, Integer>[] maps;
    static int n;
    static int[] dp;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 2);
        n = Integer.parseInt(first[0]);
        int m = Integer.parseInt(first[1]);
        maps = new HashMap[n];
        for (int i = 0; i < n; i++) {
            maps[i] = new HashMap<>();
        }
        for (int i = 0; i < m; i++) {
            String[] line = br.readLine().split(" ", 3);
            int a = Integer.parseInt(line[0]);
            int b = Integer.parseInt(line[1]);
            int score = Integer.parseInt(line[2]);
            maps[b].put(a, score);
        }
        dp = new int[1 << n];
        Arrays.fill(dp, -1);
        System.out.println(dfw((1 << n) - 1));
    }
    
    static int dfw(int key) {
        if (key == 0) {
            return 0;
        }
        if (dp[key] != -1) {
            return dp[key];
        }
        int max = 0;
        for (int i = 0; i < n; i++) {
            if (((1 << i) & key) == 0) {
                continue;
            }
            int score = 0;
            for (Map.Entry<Integer, Integer> entry : maps[i].entrySet()) {
                int x = entry.getKey();
                if (((1 << x) & key) != 0) {
                    score += entry.getValue();
                }
            }
            score += dfw(key ^ (1 << i));
            max = Math.max(max, score);
        }
        dp[key] = max;
        return max;
    }
}
0