結果

問題 No.1814 Uribo Road (Easy)
ユーザー tentententen
提出日時 2023-01-17 10:51:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 849 ms / 2,000 ms
コード長 3,328 bytes
コンパイル時間 2,378 ms
コンパイル使用メモリ 96,944 KB
実行使用メモリ 81,156 KB
最終ジャッジ日時 2024-06-10 15:49:31
合計ジャッジ時間 15,910 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
50,304 KB
testcase_01 AC 52 ms
50,324 KB
testcase_02 AC 50 ms
50,380 KB
testcase_03 AC 49 ms
50,304 KB
testcase_04 AC 48 ms
50,328 KB
testcase_05 AC 51 ms
50,300 KB
testcase_06 AC 50 ms
50,456 KB
testcase_07 AC 48 ms
50,348 KB
testcase_08 AC 49 ms
50,144 KB
testcase_09 AC 123 ms
52,692 KB
testcase_10 AC 80 ms
51,540 KB
testcase_11 AC 123 ms
52,024 KB
testcase_12 AC 159 ms
55,124 KB
testcase_13 AC 177 ms
55,632 KB
testcase_14 AC 263 ms
59,264 KB
testcase_15 AC 76 ms
50,984 KB
testcase_16 AC 83 ms
51,408 KB
testcase_17 AC 82 ms
51,500 KB
testcase_18 AC 71 ms
50,852 KB
testcase_19 AC 77 ms
51,212 KB
testcase_20 AC 318 ms
58,952 KB
testcase_21 AC 156 ms
55,012 KB
testcase_22 AC 376 ms
61,388 KB
testcase_23 AC 300 ms
60,456 KB
testcase_24 AC 479 ms
64,684 KB
testcase_25 AC 522 ms
65,084 KB
testcase_26 AC 757 ms
71,884 KB
testcase_27 AC 792 ms
75,068 KB
testcase_28 AC 843 ms
78,100 KB
testcase_29 AC 849 ms
74,892 KB
testcase_30 AC 438 ms
72,132 KB
testcase_31 AC 515 ms
81,156 KB
testcase_32 AC 298 ms
60,116 KB
testcase_33 AC 101 ms
51,544 KB
testcase_34 AC 99 ms
51,660 KB
testcase_35 AC 157 ms
54,796 KB
testcase_36 AC 160 ms
55,696 KB
testcase_37 AC 124 ms
52,268 KB
testcase_38 AC 359 ms
61,560 KB
testcase_39 AC 189 ms
55,520 KB
testcase_40 AC 196 ms
57,124 KB
testcase_41 AC 269 ms
59,648 KB
testcase_42 AC 232 ms
58,960 KB
testcase_43 AC 222 ms
56,804 KB
testcase_44 AC 461 ms
65,528 KB
testcase_45 AC 152 ms
52,888 KB
testcase_46 AC 352 ms
60,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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> roads = new HashMap<>();
        for (int i = 0; i < k; i++) {
            roads.put(sc.nextInt() - 1, i);
        }
        ArrayList<ArrayList<Path>> 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();
            int mask = 0;
            if (roads.containsKey(i)) {
                mask = (1 << roads.get(i));
            }
            graph.get(a).add(new Path(b, mask, c));
            graph.get(b).add(new Path(a, mask, c));
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(0, 0, 0));
        int[][] costs = new int[1 << k][n];
        for (int[] arr : costs) {
            Arrays.fill(arr, Integer.MAX_VALUE);
        }
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs[p.type][p.idx] <= p.value) {
                continue;
            }
            costs[p.type][p.idx] = p.value;
            for (Path x : graph.get(p.idx)) {
                queue.add(new Path(x.idx, x.type | p.type, x.value + p.value));
            }
        }
        System.out.println(costs[(1 << k) - 1][n - 1]);
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int type;
        int value;
        
        public Path(int idx, int type, int value) {
            this.idx = idx;
            this.type = type;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
        
        public String toString() {
            return idx + ":" + type + ":" + value;
        }
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0