結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー tentententen
提出日時 2021-04-14 09:12:19
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,670 bytes
コンパイル時間 3,357 ms
コンパイル使用メモリ 85,560 KB
実行使用メモリ 155,804 KB
最終ジャッジ日時 2023-09-12 22:32:39
合計ジャッジ時間 43,209 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,612 KB
testcase_01 AC 46 ms
49,416 KB
testcase_02 AC 226 ms
57,416 KB
testcase_03 AC 395 ms
63,440 KB
testcase_04 AC 218 ms
56,780 KB
testcase_05 AC 162 ms
55,440 KB
testcase_06 AC 393 ms
64,012 KB
testcase_07 AC 199 ms
56,776 KB
testcase_08 AC 353 ms
60,980 KB
testcase_09 AC 307 ms
59,612 KB
testcase_10 AC 434 ms
65,900 KB
testcase_11 AC 418 ms
60,936 KB
testcase_12 AC 2,474 ms
134,032 KB
testcase_13 AC 1,191 ms
118,960 KB
testcase_14 AC 1,961 ms
130,160 KB
testcase_15 AC 1,903 ms
127,920 KB
testcase_16 AC 2,277 ms
126,352 KB
testcase_17 TLE -
testcase_18 TLE -
testcase_19 AC 2,596 ms
136,108 KB
testcase_20 TLE -
testcase_21 TLE -
testcase_22 AC 1,181 ms
134,556 KB
testcase_23 AC 2,471 ms
155,804 KB
testcase_24 AC 1,050 ms
123,232 KB
testcase_25 AC 2,450 ms
136,856 KB
testcase_26 AC 1,283 ms
129,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static int[] depths;
    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 + k; 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);
        }
        depths = new int[n];
        parents = new int[18][n];
        costs = new long[n];
        setParents(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++) {
            int count = sc.nextInt();
            prices[i] = sc.nextInt();
            for (int j = 0; j < count; j++) {
                int a = sc.nextInt() - 1;
                graph.get(i + n).put(a, 0);
                graph.get(a).put(i + n, prices[i]);
            }
        }
        long[][] values = new long[k][n + k];
        PriorityQueue<Path> queue = new PriorityQueue<>();
        for (int i = 0; i < k; i++) {
            queue.add(new Path(n + i, 0));
            Arrays.fill(values[i], Long.MAX_VALUE);
            while (queue.size() > 0) {
                Path p = queue.poll();
                if (values[i][p.idx] <= p.value) {
                    continue;
                }
                values[i][p.idx] = p.value;
                for (int x : graph.get(p.idx).keySet()) {
                    if (values[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, values[j][left] + values[j][right] + prices[j]);
            }
            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 && depths[left] > depths[right]; 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 setParents(int idx, int p, int d, long c) {
        parents[0][idx] = p;
        depths[idx] = d;
        costs[idx] = c;
        for (int x : graph.get(idx).keySet()) {
            if (x == p) {
                continue;
            }
            setParents(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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0