結果
問題 | No.1442 I-wate Shortest Path Problem |
ユーザー | tenten |
提出日時 | 2021-04-14 09:12:19 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 2,931 ms / 3,000 ms |
コード長 | 4,670 bytes |
コンパイル時間 | 2,360 ms |
コンパイル使用メモリ | 80,520 KB |
実行使用メモリ | 151,136 KB |
最終ジャッジ日時 | 2024-06-30 10:03:11 |
合計ジャッジ時間 | 39,993 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 55 ms
50,456 KB |
testcase_01 | AC | 52 ms
50,444 KB |
testcase_02 | AC | 230 ms
56,664 KB |
testcase_03 | AC | 397 ms
64,020 KB |
testcase_04 | AC | 224 ms
56,936 KB |
testcase_05 | AC | 163 ms
55,184 KB |
testcase_06 | AC | 396 ms
61,232 KB |
testcase_07 | AC | 202 ms
55,936 KB |
testcase_08 | AC | 364 ms
60,752 KB |
testcase_09 | AC | 295 ms
58,808 KB |
testcase_10 | AC | 453 ms
63,584 KB |
testcase_11 | AC | 414 ms
61,168 KB |
testcase_12 | AC | 2,282 ms
132,792 KB |
testcase_13 | AC | 1,094 ms
114,700 KB |
testcase_14 | AC | 1,915 ms
128,148 KB |
testcase_15 | AC | 1,807 ms
122,256 KB |
testcase_16 | AC | 2,208 ms
126,552 KB |
testcase_17 | AC | 2,906 ms
141,704 KB |
testcase_18 | AC | 2,792 ms
132,564 KB |
testcase_19 | AC | 2,447 ms
131,324 KB |
testcase_20 | AC | 2,931 ms
135,468 KB |
testcase_21 | AC | 2,779 ms
138,212 KB |
testcase_22 | AC | 1,169 ms
143,640 KB |
testcase_23 | AC | 2,230 ms
151,136 KB |
testcase_24 | AC | 996 ms
117,300 KB |
testcase_25 | AC | 2,227 ms
138,936 KB |
testcase_26 | AC | 1,349 ms
131,868 KB |
ソースコード
import java.io.*; import java.util.*; public class Main { static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>(); static int[] depths; static int[][] parents; static long[] costs; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); 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]; costs = new long[n]; setParents(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 count = sc.nextInt(); prices[i] = sc.nextInt(); for (int j = 0; j < count; j++) { int a = sc.nextInt() - 1; graph.get(i + n).put(a, 0); graph.get(a).put(i + n, prices[i]); } } long[][] values = new long[k][n + k]; PriorityQueue<Path> queue = new PriorityQueue<>(); for (int i = 0; i < k; i++) { queue.add(new Path(n + i, 0)); Arrays.fill(values[i], Long.MAX_VALUE); while (queue.size() > 0) { Path p = queue.poll(); if (values[i][p.idx] <= p.value) { continue; } values[i][p.idx] = p.value; for (int x : graph.get(p.idx).keySet()) { if (values[i][x] == Long.MAX_VALUE) { 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 = costs[left] + costs[right] - costs[lca] * 2; for (int j = 0; j < k; j++) { ans = Math.min(ans, values[j][left] + values[j][right] + prices[j]); } 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 && depths[left] > depths[right]; 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 setParents(int idx, int p, int d, long c) { parents[0][idx] = p; depths[idx] = d; costs[idx] = c; for (int x : graph.get(idx).keySet()) { if (x == p) { continue; } setParents(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; } } } } 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 String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }