結果

問題 No.845 最長の切符
ユーザー GrenacheGrenache
提出日時 2019-06-29 11:37:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 331 ms / 3,000 ms
コード長 1,329 bytes
コンパイル時間 3,682 ms
コンパイル使用メモリ 73,924 KB
実行使用メモリ 66,676 KB
最終ジャッジ日時 2023-09-14 23:08:28
合計ジャッジ時間 10,474 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,696 KB
testcase_01 AC 128 ms
55,820 KB
testcase_02 AC 133 ms
55,632 KB
testcase_03 AC 128 ms
55,696 KB
testcase_04 AC 128 ms
55,796 KB
testcase_05 AC 127 ms
55,520 KB
testcase_06 AC 128 ms
55,956 KB
testcase_07 AC 127 ms
55,672 KB
testcase_08 AC 137 ms
55,616 KB
testcase_09 AC 130 ms
55,808 KB
testcase_10 AC 154 ms
55,964 KB
testcase_11 AC 144 ms
55,972 KB
testcase_12 AC 145 ms
55,952 KB
testcase_13 AC 136 ms
56,168 KB
testcase_14 AC 136 ms
55,788 KB
testcase_15 AC 199 ms
58,932 KB
testcase_16 AC 331 ms
66,060 KB
testcase_17 AC 258 ms
60,212 KB
testcase_18 AC 242 ms
61,480 KB
testcase_19 AC 218 ms
58,468 KB
testcase_20 AC 288 ms
65,296 KB
testcase_21 AC 237 ms
64,364 KB
testcase_22 AC 253 ms
58,936 KB
testcase_23 AC 215 ms
58,860 KB
testcase_24 AC 330 ms
66,676 KB
testcase_25 AC 129 ms
55,964 KB
testcase_26 AC 177 ms
64,176 KB
testcase_27 AC 129 ms
56,128 KB
testcase_28 AC 171 ms
64,344 KB
testcase_29 AC 128 ms
55,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class Main_yukicoder845 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
//		final long INF = Long.MAX_VALUE;

		int n = sc.nextInt();
		int m = sc.nextInt();

		int[][] edges = new int[n][n];
		for (int i = 0; i < m; i++) {
			int a = sc.nextInt() - 1;
			int b = sc.nextInt() - 1;
			int c = sc.nextInt();

			if (c > edges[a][b]) {
				edges[a][b] = c;
				edges[b][a] = c;
			}
		}

		long[][] dp = new long[n][0x1 << n];
		for (int i = 0; i < 0x1 << n; i++) {
			for (int j = 0; j < n; j++) {
				if ((i & 0x1 << j) == 0) {
					continue;
				}
				
				for (int k = 0; k < n; k++) {
					if (edges[j][k] == 0) {
						continue;
					}
					if ((i & 0x1 << k) != 0) {
						continue;
					}
				
					dp[k][i | 0x1 << k] = Math.max(dp[k][i | 0x1 << k], dp[j][i] + edges[j][k]);
				}
			}
		}

		long ans = 0;
		for (int i = 0; i < n; i++) {
			ans = Math.max(ans, dp[i][(0x1 << n) - 1]);
		}
		
		pr.println(ans);
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);
			
		solve();
			
		pr.close();
		sc.close();
	}

	static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}
0