結果

問題 No.845 最長の切符
ユーザー htensaihtensai
提出日時 2020-01-28 11:47:32
言語 Java21
(openjdk 21)
結果
AC  
実行時間 465 ms / 3,000 ms
コード長 1,918 bytes
コンパイル時間 2,261 ms
コンパイル使用メモリ 78,156 KB
実行使用メモリ 58,716 KB
最終ジャッジ日時 2023-10-13 14:02:58
合計ジャッジ時間 7,752 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,508 KB
testcase_01 AC 45 ms
49,864 KB
testcase_02 AC 46 ms
49,484 KB
testcase_03 AC 44 ms
49,480 KB
testcase_04 AC 44 ms
47,488 KB
testcase_05 AC 44 ms
49,376 KB
testcase_06 AC 44 ms
49,604 KB
testcase_07 AC 43 ms
51,312 KB
testcase_08 AC 49 ms
50,608 KB
testcase_09 AC 45 ms
47,280 KB
testcase_10 AC 82 ms
53,216 KB
testcase_11 AC 51 ms
50,024 KB
testcase_12 AC 60 ms
52,444 KB
testcase_13 AC 49 ms
50,604 KB
testcase_14 AC 50 ms
50,100 KB
testcase_15 AC 147 ms
53,772 KB
testcase_16 AC 449 ms
58,308 KB
testcase_17 AC 160 ms
54,068 KB
testcase_18 AC 226 ms
56,176 KB
testcase_19 AC 111 ms
53,924 KB
testcase_20 AC 445 ms
58,244 KB
testcase_21 AC 445 ms
58,520 KB
testcase_22 AC 157 ms
53,964 KB
testcase_23 AC 105 ms
54,068 KB
testcase_24 AC 465 ms
58,716 KB
testcase_25 AC 44 ms
49,760 KB
testcase_26 AC 48 ms
54,020 KB
testcase_27 AC 44 ms
49,544 KB
testcase_28 AC 51 ms
54,688 KB
testcase_29 AC 44 ms
49,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static HashMap<Integer, Integer>[] graph;
    static int[][] dp;
    static int n;
    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        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]);
        graph = new HashMap[n];
        for (int i = 0; i < n; i++) {
            graph[i] = new HashMap<>();
        }
        dp = new int[n][(int)(Math.pow(2, n))];
        for (int i = 0; i < m; i++) {
            String[] line = br.readLine().split(" ", 3);
            int a = Integer.parseInt(line[0]) - 1;
            int b = Integer.parseInt(line[1]) - 1;
            int c = Integer.parseInt(line[2]);
            if (!graph[a].containsKey(b) || graph[a].get(b) < c) {
                graph[a].put(b, c);
            }
            if (!graph[b].containsKey(a) || graph[b].get(a) < c) {
                graph[b].put(a, c);
            }
        }
        int all = (int)(Math.pow(2, n)) - 1;
        int max = 0;
        for (int i = 0; i < n; i++) {
            max = Math.max(max, dfw(i, (int)(Math.pow(2, i)) ^ all));
        }
        System.out.println(max);
   }
   
   static int dfw(int idx, int key) {
       if (key == 0) {
           return 0;
       }
       if (dp[idx][key] != 0) {
           return dp[idx][key];
       }
       int max = 0;
       for (int i = 0; i < n; i++) {
           int value = (int)(Math.pow(2, i));
           if ((value & key) == 0) {
               continue;
           }
           if (!graph[idx].containsKey(i)) {
               continue;
           }
           max = Math.max(max, dfw(i, key ^ value) + graph[idx].get(i));
       }
       dp[idx][key] = max;
       return max;
   }
}
0