結果

問題 No.1812 Uribo Road
ユーザー tentententen
提出日時 2022-01-20 13:59:32
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,150 bytes
コンパイル時間 3,483 ms
コンパイル使用メモリ 80,444 KB
実行使用メモリ 312,932 KB
最終ジャッジ日時 2024-05-02 22:44:58
合計ジャッジ時間 10,951 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
37,064 KB
testcase_01 AC 56 ms
36,792 KB
testcase_02 AC 56 ms
36,848 KB
testcase_03 AC 156 ms
41,844 KB
testcase_04 AC 55 ms
37,036 KB
testcase_05 AC 56 ms
36,924 KB
testcase_06 AC 55 ms
37,052 KB
testcase_07 AC 59 ms
37,172 KB
testcase_08 AC 213 ms
46,676 KB
testcase_09 AC 293 ms
47,988 KB
testcase_10 AC 169 ms
44,112 KB
testcase_11 AC 156 ms
40,864 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();
        HashMap<Integer, Integer> idxes = new HashMap<>();
        for (int i = 0; i < k; i++) {
            idxes.put(sc.nextInt() - 1, i);
        }
        ArrayList<ArrayList<Road>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            graph.get(a).add(new Road(b, c, idxes.getOrDefault(i, -1)));
            graph.get(b).add(new Road(a, c, idxes.getOrDefault(i, -1)));
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0, 0));
        int[][] cost = new int[1 << k][n];
        for (int[] arr : cost) {
            Arrays.fill(arr, Integer.MAX_VALUE);
        }
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (cost[p.mask][p.idx] <= p.value) {
                continue;
            }
            for (int i = p.mask; i > 0; i = (p.mask & (i - 1))) {
                cost[i][p.idx] = Math.min(cost[i][p.idx], p.value);
            }
            cost[0][p.idx] = Math.min(cost[0][p.idx], p.value);
            for (Road x : graph.get(p.idx)) {
                if (x.mask >= 0) {
                    queue.add(new Path(x.idx, p.value + x.cost, p.mask | (1 << x.mask)));
                } else {
                    queue.add(new Path(x.idx, p.value + x.cost, p.mask));
                }
            }
        }
        System.out.println(cost[(1 << k) - 1][n - 1]);
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        int mask;
        
        public Path(int idx, int value, int mask)  {
            this.idx = idx;
            this.value = value;
            this.mask = mask;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
    
    static class Road {
        int idx;
        int cost;
        int mask;
        
        public Road(int idx, int cost, int mask) {
            this.idx = idx;
            this.cost = cost;
            this.mask = mask;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0