結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー tentententen
提出日時 2024-08-06 10:21:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,391 ms / 3,000 ms
コード長 4,576 bytes
コンパイル時間 6,607 ms
コンパイル使用メモリ 83,868 KB
実行使用メモリ 111,324 KB
最終ジャッジ日時 2024-08-06 10:21:53
合計ジャッジ時間 17,904 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
38,332 KB
testcase_01 AC 72 ms
38,264 KB
testcase_02 AC 245 ms
45,860 KB
testcase_03 AC 522 ms
61,236 KB
testcase_04 AC 236 ms
45,212 KB
testcase_05 AC 194 ms
42,652 KB
testcase_06 AC 473 ms
55,764 KB
testcase_07 AC 226 ms
42,916 KB
testcase_08 AC 453 ms
54,472 KB
testcase_09 AC 368 ms
49,984 KB
testcase_10 AC 569 ms
56,196 KB
testcase_11 AC 458 ms
54,724 KB
testcase_12 AC 2,116 ms
99,808 KB
testcase_13 AC 970 ms
88,276 KB
testcase_14 AC 1,592 ms
95,316 KB
testcase_15 AC 1,432 ms
89,248 KB
testcase_16 AC 1,652 ms
91,316 KB
testcase_17 AC 2,302 ms
96,852 KB
testcase_18 AC 2,354 ms
95,444 KB
testcase_19 AC 1,882 ms
96,652 KB
testcase_20 AC 2,339 ms
95,676 KB
testcase_21 AC 2,391 ms
97,768 KB
testcase_22 AC 1,088 ms
102,508 KB
testcase_23 AC 2,053 ms
111,324 KB
testcase_24 AC 916 ms
86,428 KB
testcase_25 AC 2,179 ms
100,840 KB
testcase_26 AC 1,825 ms
94,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    static List<List<Path>> graph = new ArrayList<>();
    static int[][] parents;
    static int[] depths;
    static long[] lengths;
    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 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));
        }
        parents = new int[20][n];
        depths = new int[n];
        lengths = new long[n];
        setDepth(0, 0, 0, 0);
        for (int i = 1; i < 20; 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();
            prices[i] = sc.nextInt();
            while (m-- > 0) {
                int a = sc.nextInt() - 1;
                graph.get(n + i).add(new Path(a, 0));
                graph.get(a).add(new Path(n + i, prices[i]));
            }
        }
        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 (Path x : graph.get(p.idx)) {
                    queue.add(new Path(x.idx, p.value + x.value));
                }
            }
        }
        int q = sc.nextInt();
        String result = IntStream.range(0, q).mapToObj(x -> {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            long ans = lengths[a] + lengths[b] - lengths[getLCA(a, b)] * 2;
            for (int i = 0; i < k; i++) {
                ans = Math.min(ans, costs[i][a] + costs[i][b] + prices[i]);
            }
            return String.valueOf(ans);
        }).collect(Collectors.joining("\n"));
        System.out.println(result);
    }
    
    static void setDepth(int idx, int p, int d, long v) {
        parents[0][idx] = p;
        depths[idx] = d;
        lengths[idx] = v;
        for (Path x : graph.get(idx)) {
            if (x.idx != p) {
                setDepth(x.idx, idx, d + 1, v + x.value);
            }
        }
    }
    
    static int getLCA(int a, int b) {
        if (depths[a] < depths[b]) {
            return getLCA(b, a);
        }
        for (int i = 19; i >= 0; i--) {
            if (depths[a] - depths[b] >= (1 << i)) {
                a = parents[i][a];
            }
        }
        if (a == b) {
            return a;
        }
        for (int i = 19; i >= 0; i--) {
            if (parents[i][a] != parents[i][b]) {
                a = parents[i][a];
                b = parents[i][b];
            }
        }
        return parents[0][a];
    }
    
    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) {
            return Long.compare(value, another.value);
        }
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}


0