結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー tentententen
提出日時 2021-03-31 10:59:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,834 ms / 3,000 ms
コード長 3,986 bytes
コンパイル時間 2,412 ms
コンパイル使用メモリ 80,920 KB
実行使用メモリ 151,492 KB
最終ジャッジ日時 2024-05-08 10:37:37
合計ジャッジ時間 38,619 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
37,168 KB
testcase_01 AC 49 ms
37,220 KB
testcase_02 AC 230 ms
46,752 KB
testcase_03 AC 455 ms
50,848 KB
testcase_04 AC 215 ms
46,188 KB
testcase_05 AC 171 ms
43,072 KB
testcase_06 AC 456 ms
50,136 KB
testcase_07 AC 194 ms
43,972 KB
testcase_08 AC 415 ms
49,044 KB
testcase_09 AC 305 ms
49,024 KB
testcase_10 AC 454 ms
56,816 KB
testcase_11 AC 461 ms
49,580 KB
testcase_12 AC 2,462 ms
125,852 KB
testcase_13 AC 1,159 ms
100,476 KB
testcase_14 AC 1,711 ms
114,856 KB
testcase_15 AC 1,623 ms
114,156 KB
testcase_16 AC 2,062 ms
115,680 KB
testcase_17 AC 2,834 ms
117,320 KB
testcase_18 AC 2,725 ms
116,960 KB
testcase_19 AC 2,224 ms
119,364 KB
testcase_20 AC 2,772 ms
122,460 KB
testcase_21 AC 2,706 ms
118,684 KB
testcase_22 AC 1,084 ms
131,864 KB
testcase_23 AC 2,195 ms
151,492 KB
testcase_24 AC 992 ms
108,264 KB
testcase_25 AC 2,102 ms
124,040 KB
testcase_26 AC 1,259 ms
120,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

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) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] first = br.readLine().split(" ", 2);
		int n = Integer.parseInt(first[0]);
		int k = Integer.parseInt(first[1]);
		for (int i = 0; i < n + k; i++) {
		    graph.add(new HashMap<>());
		}
		for (int i = 0; i < n - 1; i++) {
		    String[] line = br.readLine().split(" ", 3);
		    int a = Integer.parseInt(line[0]) - 1;
		    int b = Integer.parseInt(line[1]) - 1;
		    int c = Integer.parseInt(line[2]);
		    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++) {
		    String[] line = br.readLine().split(" ", 2);
		    int m = Integer.parseInt(line[0]);
		    int p = Integer.parseInt(line[1]);
		    prices[i] = p;
		    String[] ports = br.readLine().split(" ", m);
		    for (int j = 0; j < m; j++) {
		        int x = Integer.parseInt(ports[j]) - 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()) {
		            if (costs[i][x] == Long.MAX_VALUE) {
		                queue.add(new Path(x, p.value + graph.get(p.idx).get(x)));
		            }
		        }
		    }
		}
		int q = Integer.parseInt(br.readLine());
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < q; i++) {
		    String[] line = br.readLine().split(" ", 2);
		    int left = Integer.parseInt(line[0]) - 1;
		    int right = Integer.parseInt(line[1]) - 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;
	        }
	    }
	}
}
0