結果

問題 No.845 最長の切符
ユーザー htensaihtensai
提出日時 2020-01-28 10:54:53
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,671 bytes
コンパイル時間 2,667 ms
コンパイル使用メモリ 76,588 KB
実行使用メモリ 66,128 KB
最終ジャッジ日時 2023-10-13 10:50:58
合計ジャッジ時間 21,659 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,612 KB
testcase_01 AC 125 ms
55,684 KB
testcase_02 AC 130 ms
55,804 KB
testcase_03 AC 126 ms
55,752 KB
testcase_04 AC 123 ms
55,932 KB
testcase_05 AC 125 ms
56,100 KB
testcase_06 AC 124 ms
55,716 KB
testcase_07 AC 125 ms
55,876 KB
testcase_08 AC 164 ms
57,476 KB
testcase_09 AC 130 ms
55,856 KB
testcase_10 AC 239 ms
60,552 KB
testcase_11 AC 176 ms
57,964 KB
testcase_12 AC 175 ms
57,676 KB
testcase_13 AC 167 ms
57,372 KB
testcase_14 AC 164 ms
57,208 KB
testcase_15 AC 600 ms
61,096 KB
testcase_16 TLE -
testcase_17 AC 804 ms
61,728 KB
testcase_18 AC 1,404 ms
63,392 KB
testcase_19 AC 448 ms
61,200 KB
testcase_20 AC 2,470 ms
65,460 KB
testcase_21 AC 1,234 ms
65,124 KB
testcase_22 AC 690 ms
61,512 KB
testcase_23 AC 388 ms
61,288 KB
testcase_24 TLE -
testcase_25 AC 122 ms
55,784 KB
testcase_26 AC 127 ms
60,188 KB
testcase_27 AC 122 ms
56,308 KB
testcase_28 AC 133 ms
60,452 KB
testcase_29 AC 125 ms
55,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static HashMap<Integer, Integer>[] graph;
    static int max = 0;
    static int[][] dp;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        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++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            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);
            }
        }
        for (int i = 0; i < n; i++) {
            int[] costs = new int[n];
            Arrays.fill(costs, -1);
            search(i, 0, costs);
        }
       System.out.println(max);
   }
   
   static void search(int idx, int total, int[] costs) {
       if (costs[idx] != -1) {
           return;
       }
       int key = 0;
       for (int i = 0; i < costs.length; i++) {
           key *= 2;
           if (costs[i] != -1) {
               key++;
           }
       }
       if (total != 0 && dp[idx][key] >= total) {
           return;
       }
       dp[idx][key] = total;
       max = Math.max(max, total);
       costs[idx] = total;
       for (Map.Entry<Integer, Integer> entry : graph[idx].entrySet()) {
           search(entry.getKey(), total + entry.getValue(), costs);
       }
       costs[idx] = -1;
   }
}
0