結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー tentententen
提出日時 2021-11-25 16:45:21
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,713 bytes
コンパイル時間 2,637 ms
コンパイル使用メモリ 80,692 KB
実行使用メモリ 147,168 KB
最終ジャッジ日時 2024-06-28 09:16:08
合計ジャッジ時間 41,276 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
50,128 KB
testcase_01 AC 55 ms
50,176 KB
testcase_02 AC 247 ms
56,848 KB
testcase_03 AC 400 ms
63,988 KB
testcase_04 AC 224 ms
56,772 KB
testcase_05 AC 163 ms
55,156 KB
testcase_06 AC 393 ms
64,152 KB
testcase_07 AC 201 ms
56,140 KB
testcase_08 AC 377 ms
60,840 KB
testcase_09 AC 313 ms
59,284 KB
testcase_10 AC 444 ms
65,836 KB
testcase_11 AC 417 ms
61,328 KB
testcase_12 AC 2,378 ms
133,032 KB
testcase_13 AC 1,151 ms
112,712 KB
testcase_14 AC 2,008 ms
128,360 KB
testcase_15 AC 1,742 ms
119,936 KB
testcase_16 AC 2,356 ms
129,480 KB
testcase_17 TLE -
testcase_18 AC 2,952 ms
139,516 KB
testcase_19 AC 2,440 ms
132,436 KB
testcase_20 TLE -
testcase_21 AC 2,940 ms
132,516 KB
testcase_22 AC 1,564 ms
130,284 KB
testcase_23 AC 2,532 ms
147,168 KB
testcase_24 AC 1,009 ms
113,732 KB
testcase_25 AC 2,212 ms
128,412 KB
testcase_26 AC 1,336 ms
131,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static int[] depth;
    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; 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);
        }
        depth = new int[n];
        parents = new int[17][n];
        costs = 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[] prices = new int[k];
        for (int i = 0; i < k; i++) {
            int m = sc.nextInt();
            prices[i] = sc.nextInt();
            graph.add(new HashMap<>());
            for (int j = 0; j < m; j++) {
                int x = sc.nextInt() - 1;
                graph.get(x).put(n + i, 0);
                graph.get(n + i).put(x, prices[i]);
            }
        }
        long[][] flights = new long[k][n + k];
        PriorityQueue<Path> queue = new PriorityQueue<>();
        for (int i = 0; i < k; i++) {
            Arrays.fill(flights[i], Long.MAX_VALUE);
            queue.add(new Path(n + i, 0));
            while (queue.size() > 0) {
                Path p = queue.poll();
                if (flights[i][p.idx] <= p.value) {
                    continue;
                }
                flights[i][p.idx] = p.value;
                for (int x : graph.get(p.idx).keySet()) {
                    if (flights[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, flights[j][left] + flights[j][right] - prices[j]);
            }
            sb.append(ans).append("\n");
        }
        System.out.print(sb);
    }
    
    static int getLCA(int a, int b) {
        if (depth[a] < depth[b]) {
            return getLCA(b, a);
        }
        for (int i = 16; i >= 0 && depth[a] > depth[b]; i--) {
            if (depth[a] - depth[b] >= (1 << i)) {
                a = parents[i][a];
            }
        }
        if (a == b) {
            return a;
        }
        for (int i = 16; 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 c) {
        parents[0][idx] = p;
        depth[idx] = d;
        costs[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;
            }
        }
    }
}

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 double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0