結果
問題 | No.1814 Uribo Road (Easy) |
ユーザー | tenten |
提出日時 | 2022-01-21 13:01:37 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 566 ms / 2,000 ms |
コード長 | 5,366 bytes |
コンパイル時間 | 2,240 ms |
コンパイル使用メモリ | 80,396 KB |
実行使用メモリ | 66,576 KB |
最終ジャッジ日時 | 2024-11-25 07:34:50 |
合計ジャッジ時間 | 13,422 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 51 ms
50,344 KB |
testcase_01 | AC | 52 ms
50,416 KB |
testcase_02 | AC | 51 ms
50,484 KB |
testcase_03 | AC | 50 ms
49,920 KB |
testcase_04 | AC | 51 ms
50,060 KB |
testcase_05 | AC | 52 ms
49,968 KB |
testcase_06 | AC | 51 ms
50,372 KB |
testcase_07 | AC | 51 ms
49,792 KB |
testcase_08 | AC | 52 ms
49,836 KB |
testcase_09 | AC | 114 ms
52,020 KB |
testcase_10 | AC | 102 ms
52,000 KB |
testcase_11 | AC | 128 ms
52,236 KB |
testcase_12 | AC | 196 ms
58,544 KB |
testcase_13 | AC | 193 ms
58,068 KB |
testcase_14 | AC | 238 ms
59,036 KB |
testcase_15 | AC | 74 ms
50,400 KB |
testcase_16 | AC | 86 ms
51,532 KB |
testcase_17 | AC | 89 ms
51,456 KB |
testcase_18 | AC | 65 ms
50,548 KB |
testcase_19 | AC | 62 ms
50,512 KB |
testcase_20 | AC | 278 ms
59,140 KB |
testcase_21 | AC | 182 ms
58,168 KB |
testcase_22 | AC | 364 ms
62,600 KB |
testcase_23 | AC | 296 ms
59,584 KB |
testcase_24 | AC | 406 ms
63,300 KB |
testcase_25 | AC | 370 ms
64,000 KB |
testcase_26 | AC | 445 ms
64,900 KB |
testcase_27 | AC | 566 ms
66,576 KB |
testcase_28 | AC | 540 ms
66,284 KB |
testcase_29 | AC | 495 ms
66,528 KB |
testcase_30 | AC | 346 ms
62,580 KB |
testcase_31 | AC | 333 ms
62,480 KB |
testcase_32 | AC | 213 ms
58,348 KB |
testcase_33 | AC | 112 ms
51,876 KB |
testcase_34 | AC | 99 ms
51,532 KB |
testcase_35 | AC | 186 ms
60,304 KB |
testcase_36 | AC | 132 ms
51,820 KB |
testcase_37 | AC | 157 ms
54,440 KB |
testcase_38 | AC | 296 ms
59,300 KB |
testcase_39 | AC | 196 ms
58,784 KB |
testcase_40 | AC | 193 ms
58,456 KB |
testcase_41 | AC | 303 ms
61,600 KB |
testcase_42 | AC | 246 ms
59,020 KB |
testcase_43 | AC | 193 ms
58,372 KB |
testcase_44 | AC | 270 ms
59,172 KB |
testcase_45 | AC | 143 ms
51,912 KB |
testcase_46 | AC | 360 ms
62,560 KB |
ソースコード
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(); } }