|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.OutputStreamWriter; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.PriorityQueue; |
| 10 | +import java.util.StringTokenizer; |
| 11 | + |
| 12 | +public class Main { |
| 13 | + private static BufferedReader Br; |
| 14 | + private static BufferedWriter Bw; |
| 15 | + private static StringBuilder Sb; |
| 16 | + private static int N; |
| 17 | + private static int M; |
| 18 | + private static List<Node>[] Graph; |
| 19 | + |
| 20 | + private static class Node implements Comparable<Node> { |
| 21 | + int v; |
| 22 | + int w; |
| 23 | + public Node(int v, int w) { |
| 24 | + this.v = v; |
| 25 | + this.w = w; |
| 26 | + } |
| 27 | + @Override |
| 28 | + public int compareTo(Node o) { |
| 29 | + return this.w - o.w; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static void main(String[] args) throws IOException { |
| 34 | + Br = new BufferedReader(new InputStreamReader(System.in)); |
| 35 | + Bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 36 | + Sb = new StringBuilder(); |
| 37 | + StringTokenizer st = new StringTokenizer(Br.readLine()); |
| 38 | + N = Integer.parseInt(st.nextToken()); |
| 39 | + M = Integer.parseInt(st.nextToken()); |
| 40 | + Graph = new ArrayList[N + 1]; |
| 41 | + for (int i = 1; i <= N; i++) { |
| 42 | + Graph[i] = new ArrayList<>(); |
| 43 | + } |
| 44 | + for (int i = 0; i < N - 1; i++) { |
| 45 | + st = new StringTokenizer(Br.readLine()); |
| 46 | + int a = Integer.parseInt(st.nextToken()); |
| 47 | + int b = Integer.parseInt(st.nextToken()); |
| 48 | + int w = Integer.parseInt(st.nextToken()); |
| 49 | + Graph[a].add(new Node(b, w)); |
| 50 | + Graph[b].add(new Node(a, w)); |
| 51 | + } |
| 52 | + for (int i = 0; i < M; i++) { |
| 53 | + st = new StringTokenizer(Br.readLine()); |
| 54 | + int s = Integer.parseInt(st.nextToken()); |
| 55 | + int e = Integer.parseInt(st.nextToken()); |
| 56 | + Sb.append(dijkstra(s, e)).append("\n"); |
| 57 | + } |
| 58 | + Bw.write(Sb.toString()); |
| 59 | + Bw.flush(); |
| 60 | + Bw.close(); |
| 61 | + Br.close(); |
| 62 | + } |
| 63 | + |
| 64 | + private static int dijkstra(int start, int end) { |
| 65 | + int[] dist = new int[N + 1]; |
| 66 | + for (int i = 1; i <= N; i++) |
| 67 | + dist[i] = Integer.MAX_VALUE; |
| 68 | + PriorityQueue<Node> pq = new PriorityQueue<>(); |
| 69 | + pq.add(new Node(start, 0)); |
| 70 | + dist[start] = 0; |
| 71 | + while (!pq.isEmpty()) { |
| 72 | + Node cur = pq.poll(); |
| 73 | + if (dist[cur.v] < cur.w) continue; |
| 74 | + if (cur.v == end) return cur.w; |
| 75 | + |
| 76 | + for (Node next : Graph[cur.v]) { |
| 77 | + if (dist[next.v] > cur.w + next.w) { |
| 78 | + dist[next.v] = cur.w + next.w; |
| 79 | + pq.add(new Node(next.v, dist[next.v])); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return -1; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +``` |
0 commit comments