結果

問題 No.845 最長の切符
ユーザー tenten
提出日時 2021-03-10 18:01:38
言語 Java
(openjdk 23)
結果
AC  
実行時間 279 ms / 3,000 ms
コード長 1,481 bytes
コンパイル時間 2,318 ms
コンパイル使用メモリ 79,196 KB
実行使用メモリ 60,116 KB
最終ジャッジ日時 2024-10-12 08:42:13
合計ジャッジ時間 6,880 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

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