結果

問題 No.845 最長の切符
ユーザー 37zigen37zigen
提出日時 2019-07-06 02:17:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 353 ms / 3,000 ms
コード長 1,251 bytes
コンパイル時間 2,443 ms
コンパイル使用メモリ 77,804 KB
実行使用メモリ 68,488 KB
最終ジャッジ日時 2023-10-23 21:53:51
合計ジャッジ時間 10,565 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 137 ms
57,476 KB
testcase_01 AC 140 ms
57,444 KB
testcase_02 AC 144 ms
57,696 KB
testcase_03 AC 138 ms
57,212 KB
testcase_04 AC 138 ms
57,628 KB
testcase_05 AC 134 ms
57,516 KB
testcase_06 AC 133 ms
57,476 KB
testcase_07 AC 140 ms
57,656 KB
testcase_08 AC 148 ms
57,716 KB
testcase_09 AC 140 ms
57,544 KB
testcase_10 AC 167 ms
57,628 KB
testcase_11 AC 154 ms
57,768 KB
testcase_12 AC 152 ms
57,700 KB
testcase_13 AC 139 ms
57,724 KB
testcase_14 AC 142 ms
57,552 KB
testcase_15 AC 251 ms
60,064 KB
testcase_16 AC 353 ms
68,488 KB
testcase_17 AC 275 ms
61,388 KB
testcase_18 AC 262 ms
62,476 KB
testcase_19 AC 225 ms
60,176 KB
testcase_20 AC 291 ms
67,192 KB
testcase_21 AC 248 ms
66,704 KB
testcase_22 AC 253 ms
60,688 KB
testcase_23 AC 216 ms
60,268 KB
testcase_24 AC 307 ms
68,236 KB
testcase_25 AC 131 ms
57,464 KB
testcase_26 AC 195 ms
66,080 KB
testcase_27 AC 135 ms
57,456 KB
testcase_28 AC 222 ms
66,536 KB
testcase_29 AC 134 ms
57,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {

		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int[][] dist = new int[n][n];
		int m = sc.nextInt();
		for (int i = 0; i < m; ++i) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			int c = sc.nextInt();
			--a;
			--b;
			dist[a][b] = Math.max(dist[a][b], c);
			dist[b][a] = dist[a][b];
		}

		long[][] dp = new long[1 << n][n];
		for (int i = 0; i < dp.length; ++i)
			Arrays.fill(dp[i], -Integer.MAX_VALUE / 3);
		for (int i = 0; i < n; ++i)
			dp[1 << i][i] = 0;
		for (int s = 0; s < 1 << n; ++s) {
			for (int src = 0; src < n; ++src) {
				if ((s & (1 << src)) == 0)
					continue;
				for (int dst = 0; dst < n; ++dst) {
					if ((s & (1 << dst)) > 0)
						continue;
					if (dist[src][dst] == 0)
						continue;
					dp[s | (1 << dst)][dst] = Math.max(dp[s | (1 << dst)][dst], dp[s][src] + dist[src][dst]);
				}
			}
		}
		long ans=0;
		for(int i=0;i<dp.length;++i) {
			for(long v:dp[i]) {
				ans=Math.max(ans, v);
			}
		}
		System.out.println(ans);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0