結果

問題 No.845 最長の切符
ユーザー tentententen
提出日時 2021-03-10 18:01:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 254 ms / 3,000 ms
コード長 1,481 bytes
コンパイル時間 1,920 ms
コンパイル使用メモリ 78,876 KB
実行使用メモリ 47,556 KB
最終ジャッジ日時 2024-04-20 13:16:37
合計ジャッジ時間 5,823 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
36,856 KB
testcase_01 AC 46 ms
36,920 KB
testcase_02 AC 59 ms
37,192 KB
testcase_03 AC 44 ms
36,572 KB
testcase_04 AC 46 ms
36,700 KB
testcase_05 AC 46 ms
36,576 KB
testcase_06 AC 45 ms
36,728 KB
testcase_07 AC 47 ms
37,136 KB
testcase_08 AC 55 ms
36,684 KB
testcase_09 AC 48 ms
37,072 KB
testcase_10 AC 89 ms
39,652 KB
testcase_11 AC 51 ms
36,856 KB
testcase_12 AC 75 ms
37,852 KB
testcase_13 AC 49 ms
36,900 KB
testcase_14 AC 72 ms
38,296 KB
testcase_15 AC 135 ms
40,644 KB
testcase_16 AC 216 ms
46,752 KB
testcase_17 AC 137 ms
40,924 KB
testcase_18 AC 169 ms
41,876 KB
testcase_19 AC 111 ms
40,380 KB
testcase_20 AC 216 ms
47,196 KB
testcase_21 AC 250 ms
47,228 KB
testcase_22 AC 142 ms
40,940 KB
testcase_23 AC 108 ms
40,140 KB
testcase_24 AC 254 ms
46,468 KB
testcase_25 AC 46 ms
36,728 KB
testcase_26 AC 161 ms
46,628 KB
testcase_27 AC 46 ms
36,728 KB
testcase_28 AC 165 ms
47,556 KB
testcase_29 AC 50 ms
36,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main (String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] first = br.readLine().split(" ", 2);
		int n = Integer.parseInt(first[0]);
		int m = Integer.parseInt(first[1]);
		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++) {
		    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.get(a).containsKey(b) || graph.get(a).get(b) < c) {
		        graph.get(a).put(b, c);
		    }
		    if (!graph.get(b).containsKey(a) || graph.get(b).get(a) < c) {
		        graph.get(b).put(a, c);
		    }
		}
		int[][] dp = new int[n][1 << n];
		int max = 0;
		for (int i = 1; i < (1 << n); i++) {
		    for (int j = 0; j < n; j++) {
		        if ((i & (1 << j)) == 0) {
		            continue;
		        } 
		        for (int k = 0; k < n; k++) {
		            if (k == j || (i & (1 << k)) == 0) {
		                continue;
		            }
		            if (graph.get(j).containsKey(k)) {
		                dp[j][i] = Math.max(dp[j][i], dp[k][i ^ (1 << j)] + graph.get(j).get(k));
		            }
		        }
		        max = Math.max(max, dp[j][i]);
		    }
		}
		System.out.println(max);
	}
}
0