結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー tentententen
提出日時 2022-09-02 15:43:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,891 ms / 3,000 ms
コード長 4,717 bytes
コンパイル時間 2,313 ms
コンパイル使用メモリ 80,192 KB
実行使用メモリ 121,052 KB
最終ジャッジ日時 2024-04-27 16:48:59
合計ジャッジ時間 30,143 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
37,432 KB
testcase_01 AC 45 ms
37,152 KB
testcase_02 AC 176 ms
45,492 KB
testcase_03 AC 373 ms
50,152 KB
testcase_04 AC 194 ms
45,880 KB
testcase_05 AC 137 ms
41,724 KB
testcase_06 AC 349 ms
50,220 KB
testcase_07 AC 158 ms
42,608 KB
testcase_08 AC 332 ms
49,480 KB
testcase_09 AC 275 ms
49,240 KB
testcase_10 AC 463 ms
60,696 KB
testcase_11 AC 391 ms
50,336 KB
testcase_12 AC 1,784 ms
104,800 KB
testcase_13 AC 795 ms
90,856 KB
testcase_14 AC 1,377 ms
95,848 KB
testcase_15 AC 1,137 ms
89,360 KB
testcase_16 AC 1,317 ms
92,096 KB
testcase_17 AC 1,884 ms
99,300 KB
testcase_18 AC 1,868 ms
97,236 KB
testcase_19 AC 1,621 ms
98,476 KB
testcase_20 AC 1,881 ms
99,660 KB
testcase_21 AC 1,891 ms
99,396 KB
testcase_22 AC 743 ms
105,944 KB
testcase_23 AC 1,735 ms
121,052 KB
testcase_24 AC 656 ms
87,496 KB
testcase_25 AC 1,575 ms
96,672 KB
testcase_26 AC 927 ms
99,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Path>> graph = new ArrayList<>();
    static int[] depths;
    static int[][] parents;
    static long[] prices;
    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; i++) {
            graph.add(new ArrayList<>());
        }
        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).add(new Path(b, c));
            graph.get(b).add(new Path(a, c));
        }
        depths = new int[n];
        parents = new int[17][n];
        prices = new long[n];
        setDepth(0, 0, 0, 0);
        for (int i = 1; i < 17; i++) {
            for (int j = 0; j < n; j++) {
                parents[i][j] = parents[i - 1][parents[i - 1][j]];
            }
        }
        int[] fees = new int[k];
        for (int i = 0; i < k; i++) {
            graph.add(new ArrayList<>());
            int m = sc.nextInt();
            fees[i] = sc.nextInt();
            for (int j = 0; j < m; j++) {
                int x = sc.nextInt() - 1;
                graph.get(x).add(new Path(n + i, fees[i]));
                graph.get(n + i).add(new Path(x, 0));
            }
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        long[][] costs = new long[k][n + k];
        for (int i = 0; i < k; i++) {
            queue.add(new Path(n + i, 0));
            Arrays.fill(costs[i], Long.MAX_VALUE);
            while (queue.size() > 0) {
                Path p = queue.poll();
                if (costs[i][p.idx] <= p.value) {
                    continue;
                }
                costs[i][p.idx] = p.value;
                for (Path x : graph.get(p.idx)) {
                    queue.add(new Path(x.idx, p.value + x.value));
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        int q = sc.nextInt();
        while (q-- > 0) {
            int left = sc.nextInt() - 1;
            int right = sc.nextInt() - 1;
            int lca = getLCA(left, right);
            long ans = prices[left] + prices[right] - prices[lca] * 2;
            for (int i = 0; i < k; i++) {
                ans = Math.min(ans, costs[i][left] + costs[i][right] + fees[i]);
            }
            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
    
    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;
            }
        }
    }
    
    static int getLCA(int left, int right) {
        if (depths[left] < depths[right]) {
            return getLCA(right, left);
        }
        for (int i = 16; i >= 0; i--) {
            if (depths[left] - depths[right] >= (1 << i)) {
                left = parents[i][left];
            }
        }
        if (left == right) {
            return left;
        }
        for (int i = 16; 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 v) {
        depths[idx] = d;
        parents[0][idx] = p;
        prices[idx] = v;
        for (Path x : graph.get(idx)) {
            if (x.idx == p) {
                continue;
            }
            setDepth(x.idx, idx, d + 1, v + x.value);
        }
    }
    
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0