結果

問題 No.1812 Uribo Road
ユーザー tentententen
提出日時 2022-01-21 09:30:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,102 ms / 5,000 ms
コード長 5,366 bytes
コンパイル時間 2,620 ms
コンパイル使用メモリ 80,736 KB
実行使用メモリ 128,660 KB
最終ジャッジ日時 2024-05-03 21:50:40
合計ジャッジ時間 29,105 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
37,432 KB
testcase_01 AC 47 ms
37,292 KB
testcase_02 AC 49 ms
37,156 KB
testcase_03 AC 209 ms
49,272 KB
testcase_04 AC 48 ms
37,192 KB
testcase_05 AC 47 ms
37,248 KB
testcase_06 AC 49 ms
37,256 KB
testcase_07 AC 61 ms
38,064 KB
testcase_08 AC 173 ms
44,956 KB
testcase_09 AC 243 ms
47,820 KB
testcase_10 AC 195 ms
45,996 KB
testcase_11 AC 131 ms
39,964 KB
testcase_12 AC 2,372 ms
128,660 KB
testcase_13 AC 238 ms
54,468 KB
testcase_14 AC 264 ms
51,636 KB
testcase_15 AC 785 ms
57,488 KB
testcase_16 AC 707 ms
59,056 KB
testcase_17 AC 1,569 ms
63,820 KB
testcase_18 AC 1,493 ms
59,216 KB
testcase_19 AC 2,853 ms
83,540 KB
testcase_20 AC 2,392 ms
72,252 KB
testcase_21 AC 3,102 ms
111,172 KB
testcase_22 AC 2,227 ms
109,760 KB
testcase_23 AC 427 ms
54,224 KB
testcase_24 AC 58 ms
37,512 KB
testcase_25 AC 307 ms
50,860 KB
testcase_26 AC 186 ms
44,928 KB
testcase_27 AC 1,301 ms
58,120 KB
testcase_28 AC 434 ms
55,568 KB
testcase_29 AC 51 ms
36,776 KB
testcase_30 AC 452 ms
52,968 KB
testcase_31 AC 1,559 ms
59,420 KB
testcase_32 AC 216 ms
49,232 KB
testcase_33 AC 1,007 ms
69,088 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