結果
問題 | No.1442 I-wate Shortest Path Problem |
ユーザー | tenten |
提出日時 | 2021-03-31 10:51:26 |
言語 | Java21 (openjdk 21) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,438 bytes |
コンパイル時間 | 3,148 ms |
コンパイル使用メモリ | 89,944 KB |
実行使用メモリ | 274,832 KB |
最終ジャッジ日時 | 2024-12-14 13:44:20 |
合計ジャッジ時間 | 65,397 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 137 ms
46,604 KB |
testcase_01 | AC | 127 ms
169,608 KB |
testcase_02 | AC | 470 ms
177,580 KB |
testcase_03 | AC | 931 ms
64,260 KB |
testcase_04 | AC | 441 ms
57,080 KB |
testcase_05 | AC | 324 ms
177,740 KB |
testcase_06 | AC | 922 ms
64,672 KB |
testcase_07 | AC | 374 ms
60,360 KB |
testcase_08 | AC | 859 ms
62,784 KB |
testcase_09 | AC | 633 ms
63,776 KB |
testcase_10 | AC | 1,024 ms
65,132 KB |
testcase_11 | AC | 947 ms
61,020 KB |
testcase_12 | TLE | - |
testcase_13 | AC | 2,175 ms
114,968 KB |
testcase_14 | TLE | - |
testcase_15 | AC | 2,756 ms
118,872 KB |
testcase_16 | TLE | - |
testcase_17 | TLE | - |
testcase_18 | TLE | - |
testcase_19 | TLE | - |
testcase_20 | TLE | - |
testcase_21 | TLE | - |
testcase_22 | AC | 2,131 ms
140,900 KB |
testcase_23 | TLE | - |
testcase_24 | AC | 1,976 ms
118,904 KB |
testcase_25 | TLE | - |
testcase_26 | AC | 2,430 ms
274,832 KB |
ソースコード
import java.util.*; public class Main { static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>(); static int[] depths; static int[][] parents; static long[] totals; public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); for (int i = 0; i < n + k; i++) { graph.add(new HashMap<>()); } for (int i = 0; i < n - 1; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; int c = sc.nextInt(); graph.get(a).put(b, c); graph.get(b).put(a, c); } depths = new int[n]; parents = new int[18][n]; totals = new long[n]; setDepth(0, 0, 0, 0); for (int i = 1; i < 18; i++) { for (int j = 0; j < n; j++) { parents[i][j] = parents[i - 1][parents[i - 1][j]]; } } int[] prices = new int[k]; for (int i = 0; i < k; i++) { int m = sc.nextInt(); int p = sc.nextInt(); prices[i] = p; for (int j = 0; j < m; j++) { int x = sc.nextInt() - 1; graph.get(x).put(n + i, p); graph.get(n + i).put(x, 0); } } PriorityQueue<Path> queue = new PriorityQueue<>(); long[][] costs = new long[k][n + k]; for (int i = 0; i < k; i++) { Arrays.fill(costs[i], Long.MAX_VALUE); queue.add(new Path(n + i, 0)); while (queue.size() > 0) { Path p = queue.poll(); if (costs[i][p.idx] <= p.value) { continue; } costs[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))); } } } int q = sc.nextInt(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < q; i++) { int left = sc.nextInt() - 1; int right = sc.nextInt() - 1; int lca = getLCA(left, right); long ans = totals[left] + totals[right] - totals[lca] * 2; for (int j = 0; j < k; j++) { long tmp = costs[j][left] + costs[j][right] + prices[j]; ans = Math.min(ans, tmp); } sb.append(ans).append("\n"); } System.out.print(sb); } static int getLCA(int left, int right) { if (depths[left] < depths[right]) { return getLCA(right, left); } for (int i = 17; i >= 0; i--) { if (depths[left] - depths[right] >= (1 << i)) { left = parents[i][left]; } } if (left == right) { return left; } for (int i = 17; i >= 0; i--) { if (parents[i][left] != parents[i][right]) { left = parents[i][left]; right = parents[i][right]; } } return parents[0][left]; } static void setDepth(int idx, int p, int d, long c) { depths[idx] = d; parents[0][idx] = p; totals[idx] = c; for (int x : graph.get(idx).keySet()) { if (x == p) { continue; } setDepth(x, idx, d + 1, c + graph.get(idx).get(x)); } } static class Path implements Comparable<Path> { int idx; long value; public Path(int idx, long value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { if (value == another.value) { return 0; } else if (value < another.value) { return -1; } else { return 1; } } } }