結果

問題 No.1814 Uribo Road (Easy)
ユーザー tentententen
提出日時 2022-01-21 13:01:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 544 ms / 2,000 ms
コード長 5,366 bytes
コンパイル時間 2,377 ms
コンパイル使用メモリ 81,592 KB
実行使用メモリ 56,704 KB
最終ジャッジ日時 2024-05-04 01:45:16
合計ジャッジ時間 13,157 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
37,384 KB
testcase_01 AC 48 ms
37,028 KB
testcase_02 AC 49 ms
37,196 KB
testcase_03 AC 48 ms
36,940 KB
testcase_04 AC 48 ms
37,064 KB
testcase_05 AC 48 ms
36,944 KB
testcase_06 AC 49 ms
37,100 KB
testcase_07 AC 49 ms
37,476 KB
testcase_08 AC 48 ms
36,936 KB
testcase_09 AC 109 ms
39,332 KB
testcase_10 AC 99 ms
39,212 KB
testcase_11 AC 126 ms
40,020 KB
testcase_12 AC 182 ms
45,788 KB
testcase_13 AC 177 ms
45,336 KB
testcase_14 AC 228 ms
47,668 KB
testcase_15 AC 74 ms
37,728 KB
testcase_16 AC 72 ms
38,836 KB
testcase_17 AC 89 ms
38,912 KB
testcase_18 AC 56 ms
37,624 KB
testcase_19 AC 58 ms
37,708 KB
testcase_20 AC 268 ms
49,492 KB
testcase_21 AC 166 ms
44,556 KB
testcase_22 AC 365 ms
51,608 KB
testcase_23 AC 276 ms
51,660 KB
testcase_24 AC 379 ms
51,772 KB
testcase_25 AC 374 ms
53,292 KB
testcase_26 AC 442 ms
55,448 KB
testcase_27 AC 544 ms
56,472 KB
testcase_28 AC 521 ms
56,252 KB
testcase_29 AC 507 ms
56,704 KB
testcase_30 AC 352 ms
53,312 KB
testcase_31 AC 331 ms
51,860 KB
testcase_32 AC 201 ms
46,120 KB
testcase_33 AC 108 ms
39,144 KB
testcase_34 AC 93 ms
39,064 KB
testcase_35 AC 190 ms
45,792 KB
testcase_36 AC 120 ms
39,976 KB
testcase_37 AC 156 ms
40,980 KB
testcase_38 AC 261 ms
47,828 KB
testcase_39 AC 177 ms
44,840 KB
testcase_40 AC 184 ms
45,824 KB
testcase_41 AC 293 ms
50,976 KB
testcase_42 AC 240 ms
48,692 KB
testcase_43 AC 185 ms
45,092 KB
testcase_44 AC 262 ms
48,316 KB
testcase_45 AC 134 ms
40,004 KB
testcase_46 AC 387 ms
53,356 KB
権限があれば一括ダウンロードができます

ソースコード

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<HashMap<Integer, Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new HashMap<>());
        }
        HashMap<Integer, Integer> compress = new HashMap<>();
        int[] lefts = new int[k];
        int[] rights = new int[k];
        int[] values = new int[k];
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            if (idxes.containsKey(i)) {
                int x = idxes.get(i);
                lefts[x] = a;
                rights[x] = b;
                values[x] = c;
                compress.put(a, null);
                compress.put(b, null);
            } else {
                graph.get(a).put(b, c);
                graph.get(b).put(a, c);
            }
        }
        compress.put(0, null);
        compress.put(n - 1, null);
        int count = 0;
        int[] cities = new int[compress.size()];
        for (int x : compress.keySet()) {
            cities[count] = x;
            compress.put(x, count++);
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        int[][] cost = new int[count][n];
        for (int i = 0; i < count; i++) {
            Arrays.fill(cost[i], Integer.MAX_VALUE);
            queue.add(new Path(cities[i], 0));
            while (queue.size() > 0) {
                Path p = queue.poll();
                if (cost[i][p.idx] <= p.value) {
                    continue;
                }
                cost[i][p.idx] = p.value;
                for (int x : graph.get(p.idx).keySet()) {
                    queue.add(new Path(x, p.value + graph.get(p.idx).get(x)));
                }
            }
        }
        ArrayList<ArrayList<Road>> small = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            small.add(new ArrayList<>());
        }
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < count; j++) {
                if (i == j) {
                    continue;
                }
                small.get(i).add(new Road(j, cost[i][cities[j]], -1));
            }
        }
        for (int i = 0; i < k; i++) {
            small.get(compress.get(lefts[i])).add(new Road(compress.get(rights[i]), values[i], i));
            small.get(compress.get(rights[i])).add(new Road(compress.get(lefts[i]), values[i], i));
        }
        long[][] ans = new long[1 << k][count];
        for (long[] arr : ans) {
            Arrays.fill(arr, Long.MAX_VALUE);
        }
        PriorityQueue<Road> roads = new PriorityQueue<>();
        roads.add(new Road(compress.get(0), 0, 0));
        while (roads.size() > 0) {
            Road r = roads.poll();
            if (ans[r.mask][r.idx] <= r.value) {
                continue;
            }
            for (int i = r.mask; i > 0; i = (r.mask & (i - 1))) {
                ans[i][r.idx] = Math.min(ans[i][r.idx], r.value);
            }
            ans[0][r.idx] = Math.min(ans[0][r.idx], r.value);
            for (Road x : small.get(r.idx)) {
                if (x.mask >= 0) {
                    roads.add(new Road(x.idx, r.value + x.value, r.mask | (1 << x.mask)));
                } else {
                    roads.add(new Road(x.idx, r.value + x.value, r.mask));
                }
            }
        }
        System.out.println(ans[(1 << k) - 1][compress.get(n - 1)]);
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;

        public Path(int idx, int value)  {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
    
    static class Road implements Comparable<Road> {
        int idx;
        long value;
        int mask;
        
        public Road(int idx, long value, int mask) {
            this.idx = idx;
            this.value = value;
            this.mask = mask;
        }
        
        public int compareTo(Road another) {
            if (value == another.value) {
                return 0;
            } else if (value < another.value) {
                return -1;
            } else {
                return 1;
            }
        }
    }
}

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