Let's talk about Chinese questions directly!
Idea: I'm excited when I look at the question. It seems to be a problem related to the maximum stream. But after carefully reviewing the question, I find that you need to find the path with the smallest difference between the maximum speed and the minimum speed! The difference between the maximum and the minimum is the smallest, so we can think of the greedy problem. Another point is that we can reach the destination, which means we need to connect the given start point and end point, so we can consider and check the idea of the set!
So the general idea of this question can be determined as: We can sort the weights of all edges, and then enumerate all vertices from 0 to connect all vertices, check whether "find (a)" is equal to "find (B)". If it is equal to "find (B)", we can take the maximum value and the minimum value as the difference!
AC code:
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define INF limit int f [205], n; struct P {int U, V, w;} num [1005]; bool CMP (P x, p y) {return X. W <Y. w;} int find (int x) {If (X! = F [x]) x = find (F [x]); Return X;} int main () {int M, T, I, J, K, Minn; while (scanf ("% d", & N, & M )! = EOF) {for (I = 0; I <m; I ++) scanf ("% d", & num [I]. u, & num [I]. v, & num [I]. w); sort (Num, num + M, CMP); scanf ("% d", & T); While (t --) {Minn = inf; int A, B; scanf ("% d", & A, & B); for (I = 0; I <m; I ++) // starts enumeration from 0th points, find the optimal solution for all cases {for (k = 1; k <= N; k ++) // each enumeration determines a new query set f [k] = K; For (j = I; j <m; j ++) // start from vertex I to find the path {int X, Y; X = find (Num [J]. u); y = find (Num [J]. v); If (X! = Y) f [y] = x; If (find (A) = find (B) {If (Minn> num [J]. w-num [I]. w) Minn = num [J]. w-num [I]. w; break;} If (j = m) // The first time we connect to all vertices, we find that there is no path from A to B, so we do not need to find a new break ;}} if (Minn = 100000000) printf ("-1 \ n"); else printf ("% d \ n", Minn) ;}} return 0 ;}