結果

問題 No.845 最長の切符
ユーザー Grenache
提出日時 2019-06-29 11:37:29
言語 Java
(openjdk 23)
結果
AC  
実行時間 342 ms / 3,000 ms
コード長 1,329 bytes
コンパイル時間 3,402 ms
コンパイル使用メモリ 77,112 KB
実行使用メモリ 54,788 KB
最終ジャッジ日時 2024-07-02 05:29:14
合計ジャッジ時間 9,868 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

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