結果
| 問題 | No.845 最長の切符 | 
| コンテスト | |
| ユーザー |  htensai | 
| 提出日時 | 2020-01-28 11:47:32 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 469 ms / 3,000 ms | 
| コード長 | 1,918 bytes | 
| コンパイル時間 | 2,821 ms | 
| コンパイル使用メモリ | 79,060 KB | 
| 実行使用メモリ | 48,224 KB | 
| 最終ジャッジ日時 | 2024-09-15 10:50:08 | 
| 合計ジャッジ時間 | 7,727 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 27 | 
ソースコード
import java.util.*;
import java.io.*;
public class Main {
    static HashMap<Integer, Integer>[] graph;
    static int[][] dp;
    static int n;
    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 2);
        n = Integer.parseInt(first[0]);
        int m = Integer.parseInt(first[1]);
        graph = new HashMap[n];
        for (int i = 0; i < n; i++) {
            graph[i] = new HashMap<>();
        }
        dp = new int[n][(int)(Math.pow(2, n))];
        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[a].containsKey(b) || graph[a].get(b) < c) {
                graph[a].put(b, c);
            }
            if (!graph[b].containsKey(a) || graph[b].get(a) < c) {
                graph[b].put(a, c);
            }
        }
        int all = (int)(Math.pow(2, n)) - 1;
        int max = 0;
        for (int i = 0; i < n; i++) {
            max = Math.max(max, dfw(i, (int)(Math.pow(2, i)) ^ all));
        }
        System.out.println(max);
   }
   
   static int dfw(int idx, int key) {
       if (key == 0) {
           return 0;
       }
       if (dp[idx][key] != 0) {
           return dp[idx][key];
       }
       int max = 0;
       for (int i = 0; i < n; i++) {
           int value = (int)(Math.pow(2, i));
           if ((value & key) == 0) {
               continue;
           }
           if (!graph[idx].containsKey(i)) {
               continue;
           }
           max = Math.max(max, dfw(i, key ^ value) + graph[idx].get(i));
       }
       dp[idx][key] = max;
       return max;
   }
}
            
            
            
        