|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static PriorityQueue<int[]> pq; |
| 9 | + private static List<int[]>[] edges; |
| 10 | + private static int[] uf, size; |
| 11 | + private static boolean[] visited; |
| 12 | + private static int N, M, K; |
| 13 | +
|
| 14 | + public static void main(String[] args) throws IOException { |
| 15 | + init(); |
| 16 | + kruskal(); |
| 17 | +
|
| 18 | + while (K-->0) { |
| 19 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 20 | + int start = Integer.parseInt(st.nextToken()); |
| 21 | + int end = Integer.parseInt(st.nextToken()); |
| 22 | +
|
| 23 | + int answer = BFS(start, end); |
| 24 | + bw.write(answer + "\n"); |
| 25 | + } |
| 26 | +
|
| 27 | + bw.flush(); |
| 28 | + bw.close(); |
| 29 | + br.close(); |
| 30 | + } |
| 31 | +
|
| 32 | + private static void init() throws IOException { |
| 33 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 34 | + N = Integer.parseInt(st.nextToken()); |
| 35 | + M = Integer.parseInt(st.nextToken()); |
| 36 | + K = Integer.parseInt(st.nextToken()); |
| 37 | +
|
| 38 | + uf = new int[N+1]; |
| 39 | + size = new int[N+1]; |
| 40 | + visited = new boolean[N+1]; |
| 41 | + pq = new PriorityQueue<>((o1, o2) -> Integer.compare(o2[2], o1[2])); |
| 42 | + edges = new List[N+1]; |
| 43 | +
|
| 44 | + for (int i = 1; i <= N; i++) { |
| 45 | + uf[i] = i; |
| 46 | + size[i] = 1; |
| 47 | + edges[i] = new ArrayList<>(); |
| 48 | + } |
| 49 | +
|
| 50 | + for (int a = 0; a < M; a++) { |
| 51 | + st = new StringTokenizer(br.readLine()); |
| 52 | + int i = Integer.parseInt(st.nextToken()); |
| 53 | + int j = Integer.parseInt(st.nextToken()); |
| 54 | + int w = Integer.parseInt(st.nextToken()); |
| 55 | + pq.add(new int[] {i, j, w}); |
| 56 | + } |
| 57 | + } |
| 58 | +
|
| 59 | + private static int BFS(int start, int end) { |
| 60 | + Queue<int[]> q = new ArrayDeque<>(); |
| 61 | + Arrays.fill(visited, false); |
| 62 | + visited[start] = true; |
| 63 | + q.add(new int[] {start, 200}); |
| 64 | +
|
| 65 | + while (!q.isEmpty()) { |
| 66 | + int[] current = q.poll(); |
| 67 | +
|
| 68 | + if (current[0] == end) { |
| 69 | + return current[1]; |
| 70 | + } |
| 71 | +
|
| 72 | + for (int[] edge : edges[current[0]]) { |
| 73 | + if (visited[edge[0]]) continue; |
| 74 | + visited[edge[0]] = true; |
| 75 | + q.add(new int[] {edge[0], Math.min(current[1], edge[1])}); |
| 76 | + } |
| 77 | + } |
| 78 | +
|
| 79 | + return -1; |
| 80 | + } |
| 81 | +
|
| 82 | + private static void kruskal() { |
| 83 | + int edgeCount = 0; |
| 84 | +
|
| 85 | + while (!pq.isEmpty() && edgeCount + 1 <= N) { |
| 86 | + int[] edge = pq.poll(); |
| 87 | +
|
| 88 | + int i = find(edge[0]); |
| 89 | + int j = find(edge[1]); |
| 90 | +
|
| 91 | + if (i == j) continue; |
| 92 | + union(i, j); |
| 93 | + edges[i].add(new int[]{j, edge[2]}); |
| 94 | + edges[j].add(new int[]{i, edge[2]}); |
| 95 | + edgeCount++; |
| 96 | + } |
| 97 | + } |
| 98 | +
|
| 99 | + private static void union(int x, int y) { |
| 100 | + int X = find(x); |
| 101 | + int Y = find(y); |
| 102 | +
|
| 103 | + if (size[X] < size[Y]) { |
| 104 | + uf[X] = Y; |
| 105 | + size[Y] += size[X]; |
| 106 | + } else { |
| 107 | + uf[Y] = X; |
| 108 | + size[X] += size[Y]; |
| 109 | + } |
| 110 | + } |
| 111 | +
|
| 112 | + private static int find(int x) { |
| 113 | + if (uf[x] == x) return x; |
| 114 | +
|
| 115 | + return uf[x] = find(uf[x]); |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
0 commit comments