結果

問題 No.845 最長の切符
ユーザー tentententen
提出日時 2020-09-01 08:30:34
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,440 bytes
コンパイル時間 2,775 ms
コンパイル使用メモリ 80,032 KB
実行使用メモリ 64,124 KB
最終ジャッジ日時 2024-04-28 21:31:02
合計ジャッジ時間 10,282 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
53,784 KB
testcase_01 AC 134 ms
53,996 KB
testcase_02 AC 152 ms
54,204 KB
testcase_03 AC 130 ms
54,188 KB
testcase_04 AC 133 ms
54,164 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 245 ms
58,984 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int n = sc.nextInt();
    	int m = sc.nextInt();
    	ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    	for (int i = 0; i < n; i++) {
    	    graph.add(new HashMap<>());
    	}
    	for (int i = 0; i < m; i++) {
    	    int a = sc.nextInt() - 1;
    	    int b = sc.nextInt() - 1;
    	    int c = sc.nextInt();
    	    if (!graph.get(a).containsKey(b) || graph.get(a).get(b) < c) {
    	        graph.get(a).put(b, c);
    	        graph.get(b).put(a, c);
    	    }
    	}
    	int[][] dp = new int[1 << n][n];
    	int max = 0;
    	for (int i = 1; i < (1 << n) - 1; i++) {
    	    for (int j = 0; j < n; j++) {
    	        if ((i & (1 << j)) == 0) {
    	            continue;
    	        }
    	        int tmp = 0;
    	        for (int k = 0; k < n; k++) {
    	            if (j == k) {
    	                continue;
    	            }
    	            if ((i & (1 << k)) == 0) {
    	                continue;
    	            }
    	            if (!graph.get(j).containsKey(k)) {
    	                continue;
    	            }
    	            tmp = Math.max(tmp, dp[i ^ (1 << j) ^ (1 << k)][k] + graph.get(j).get(k));
    	        }
    	        dp[i ^ (1 << j)][j] = tmp;
    	        max = Math.max(max, tmp);
    	    }
    	} 
    	System.out.println(max);
    }
}
0