結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー tentententen
提出日時 2023-04-12 09:01:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,675 ms / 3,000 ms
コード長 5,143 bytes
コンパイル時間 3,281 ms
コンパイル使用メモリ 92,296 KB
実行使用メモリ 115,844 KB
最終ジャッジ日時 2024-04-16 23:32:37
合計ジャッジ時間 40,038 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
36,960 KB
testcase_01 AC 56 ms
37,492 KB
testcase_02 AC 235 ms
45,568 KB
testcase_03 AC 431 ms
50,144 KB
testcase_04 AC 228 ms
45,304 KB
testcase_05 AC 173 ms
41,976 KB
testcase_06 AC 458 ms
50,260 KB
testcase_07 AC 209 ms
42,964 KB
testcase_08 AC 448 ms
49,580 KB
testcase_09 AC 389 ms
49,700 KB
testcase_10 AC 523 ms
50,172 KB
testcase_11 AC 461 ms
50,408 KB
testcase_12 AC 2,177 ms
93,768 KB
testcase_13 AC 1,007 ms
78,668 KB
testcase_14 AC 1,680 ms
90,908 KB
testcase_15 AC 1,506 ms
83,084 KB
testcase_16 AC 1,848 ms
86,456 KB
testcase_17 AC 2,647 ms
93,848 KB
testcase_18 AC 2,598 ms
92,084 KB
testcase_19 AC 2,229 ms
90,652 KB
testcase_20 AC 2,665 ms
92,032 KB
testcase_21 AC 2,675 ms
92,204 KB
testcase_22 AC 1,014 ms
100,344 KB
testcase_23 AC 2,367 ms
115,844 KB
testcase_24 AC 945 ms
81,416 KB
testcase_25 AC 2,040 ms
92,672 KB
testcase_26 AC 1,151 ms
87,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Path>> graph = new ArrayList<>();
    static int[] depths;
    static int[][] parents;
    static long[] values;
    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[18][n];
        values = 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++) {
            graph.add(new ArrayList<>());
            int m = sc.nextInt();
            prices[i] = sc.nextInt();
            for (int j = 0; j < m; j++) {
                int x = sc.nextInt() - 1;
                graph.get(n + i).add(new Path(x, 0));
                graph.get(x).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++) {
            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, x.value + p.value));
                }
            }
        }
        int q = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (q-- > 0) {
            int left = sc.nextInt() - 1;
            int right = sc.nextInt() - 1;
            int lca = getLCA(left, right);
            long ans = values[left] + values[right] - values[lca] * 2;
            for (int i = 0; i < k; i++) {
                ans = Math.min(ans, costs[i][left] + costs[i][right] + prices[i]);
            }
            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
    
    static int getLCA(int a, int b) {
        if (depths[a] < depths[b]) {
            return getLCA(b, a);
        }
        for (int i = 17; i >= 0; i--) {
            if (depths[a] - depths[b] >= (1 << i)) {
                a = parents[i][a];
            }
        }
        if (a == b) {
            return a;
        }
        for (int i = 17; i >= 0; i--) {
            if (parents[i][a] != parents[i][b]) {
                a = parents[i][a];
                b = parents[i][b];
            }
        }
        return parents[0][a];
    }
    
    static void setDepth(int idx, int p, int d, long v) {
        depths[idx] = d;
        parents[0][idx] = p;
        values[idx] = v;
        for (Path x : graph.get(idx)) {
            if (x.idx != p) {
                setDepth(x.idx, idx, d + 1, v + x.value);
            }
        }
    }
    
    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 Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0