|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) |
| 24 | + nextLine(); |
| 25 | + return st.nextToken(); |
| 26 | + } |
| 27 | + |
| 28 | + int nextInt() throws Exception { |
| 29 | + return Integer.parseInt(nextToken()); |
| 30 | + } |
| 31 | + |
| 32 | + long nextLong() throws Exception { |
| 33 | + return Long.parseLong(nextToken()); |
| 34 | + } |
| 35 | + |
| 36 | + double nextDouble() throws Exception { |
| 37 | + return Double.parseDouble(nextToken()); |
| 38 | + } |
| 39 | + |
| 40 | + void close() throws Exception { |
| 41 | + bw.flush(); |
| 42 | + bw.close(); |
| 43 | + } |
| 44 | + |
| 45 | + void write(String content) throws Exception { |
| 46 | + bw.write(content); |
| 47 | + } |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +public class Main { |
| 52 | + |
| 53 | + static IOController io; |
| 54 | + |
| 55 | + // |
| 56 | + |
| 57 | + static final int INF = (int)1e9 + 7; |
| 58 | + |
| 59 | + static int N, M, K, Q; |
| 60 | + static List<int[]>[] graph; |
| 61 | + static List<Integer>[] tree; |
| 62 | + static List<int[]> edges; |
| 63 | + static int[][] min, par; |
| 64 | + static int[] dep, root, dist; |
| 65 | + |
| 66 | + public static int f(int x) { return x == root[x] ? x : (root[x] = f(root[x])); } |
| 67 | + |
| 68 | + public static void main(String[] args) throws Exception { |
| 69 | + |
| 70 | + io = new IOController(); |
| 71 | + |
| 72 | + N = io.nextInt(); |
| 73 | + M = io.nextInt(); |
| 74 | + K = io.nextInt(); |
| 75 | + Q = io.nextInt(); |
| 76 | + graph = new List[N+1]; |
| 77 | + tree = new List[N+1]; |
| 78 | + root = new int[N+1]; |
| 79 | + for(int i=1;i<=N;i++) { |
| 80 | + graph[i] = new ArrayList<>(); |
| 81 | + tree[i] = new ArrayList<>(); |
| 82 | + root[i] = i; |
| 83 | + } |
| 84 | + |
| 85 | + edges = new ArrayList<>(); |
| 86 | + for(int i=1;i<=M;i++) { |
| 87 | + int a = io.nextInt(); |
| 88 | + int b = io.nextInt(); |
| 89 | + int c = io.nextInt(); |
| 90 | + graph[a].add(new int[]{b,c}); |
| 91 | + graph[b].add(new int[]{a,c}); |
| 92 | + edges.add(new int[]{a,b,0}); |
| 93 | + } |
| 94 | + |
| 95 | + int[] starts = new int[K]; |
| 96 | + for(int i=0;i<K;i++) starts[i] = io.nextInt(); |
| 97 | + dijkstra(starts); |
| 98 | + |
| 99 | + |
| 100 | + for(int[] edge : edges) edge[2] = Math.min(dist[edge[0]], dist[edge[1]]); |
| 101 | + Collections.sort(edges, (a,b) -> b[2]-a[2]); |
| 102 | + |
| 103 | + for(int[] edge : edges) { |
| 104 | + int a = edge[0], b = edge[1], c = edge[2]; |
| 105 | + int x = f(a), y = f(b); |
| 106 | + if(x == y) continue; |
| 107 | + tree[a].add(b); |
| 108 | + tree[b].add(a); |
| 109 | + root[x] = y; |
| 110 | + } |
| 111 | + |
| 112 | + par = new int[N+1][17]; |
| 113 | + min = new int[N+1][17]; |
| 114 | + dep = new int[N+1]; |
| 115 | + dfs(1,0,0); |
| 116 | + |
| 117 | + for(int k=1;k<17;k++) for(int i=1;i<=N;i++) { |
| 118 | + par[i][k] = par[par[i][k-1]][k-1]; |
| 119 | + min[i][k] = Math.min(min[i][k-1], min[par[i][k-1]][k-1]); |
| 120 | + } |
| 121 | + |
| 122 | + while(Q-->0) { |
| 123 | + int a = io.nextInt(); |
| 124 | + int b = io.nextInt(); |
| 125 | + int ans = Integer.MAX_VALUE; |
| 126 | + |
| 127 | + int diff = Math.abs(dep[a] - dep[b]); |
| 128 | + for(int k=0;k<17;k++) if((diff & (1<<k)) != 0) { |
| 129 | + if(dep[a] > dep[b]) { |
| 130 | + ans = Math.min(ans, min[a][k]); |
| 131 | + a = par[a][k]; |
| 132 | + } |
| 133 | + else { |
| 134 | + ans = Math.min(ans, min[b][k]); |
| 135 | + b = par[b][k]; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + for(int k=16;k>=0;k--) if(par[a][k] != par[b][k]) { |
| 140 | + ans = Math.min(ans, Math.min(min[a][k], min[b][k])); |
| 141 | + a = par[a][k]; |
| 142 | + b = par[b][k]; |
| 143 | + } |
| 144 | + |
| 145 | + if(a != b) { |
| 146 | + ans = Math.min(ans, Math.min(min[a][0], min[b][0])); |
| 147 | + a = par[a][0]; |
| 148 | + } |
| 149 | + ans = Math.min(ans, dist[a]); |
| 150 | + |
| 151 | + io.write(ans + "\n"); |
| 152 | + } |
| 153 | + |
| 154 | + io.close(); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + public static void dijkstra(int[] starts) { |
| 159 | + dist = new int[N+1]; |
| 160 | + Arrays.fill(dist, INF); |
| 161 | + PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> a[0]-b[0]); |
| 162 | + for(int i=0;i<starts.length;i++) { |
| 163 | + dist[starts[i]] = 0; |
| 164 | + pq.offer(new int[]{0, starts[i]}); |
| 165 | + } |
| 166 | + |
| 167 | + while(!pq.isEmpty()) { |
| 168 | + int[] cur = pq.poll(); |
| 169 | + int d = cur[0], n = cur[1]; |
| 170 | + if(d > dist[n]) continue; |
| 171 | + for(int[] e:graph[n]) if(dist[e[0]] > d + e[1]) { |
| 172 | + dist[e[0]] = d + e[1]; |
| 173 | + pq.offer(new int[]{dist[e[0]], e[0]}); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + public static void dfs(int n, int p, int d) { |
| 179 | + dep[n] = d; |
| 180 | + par[n][0] = p; |
| 181 | + min[n][0] = dist[n]; |
| 182 | + for(int i:tree[n]) if(i != p) dfs(i, n, d+1); |
| 183 | + } |
| 184 | + |
| 185 | +} |
| 186 | +``` |
0 commit comments