Find the most comfortable roadtime limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3686 accepted submission (s): 1565
Problem descriptionxx has many cities that communicate with each other through a strange highway SARS (super air roam structure --- super Air Roaming structure, each piece of SARS imposes a fixed speed limit on the flycar driving. Meanwhile, XX has special requirements on flycar's "comfort, that is, the lower the speed difference between the highest speed and the lowest speed, the more comfortable the ride. (It is understood that flycar must speed up/speed down instantly, which is painful ),
However, XX has less requirements on time. You need to find the most comfortable path between cities. (SARS is bidirectional ).
Input includes multiple test instances, each of which includes:
The first line has two positive integers n (1 <n <= 200) and M (M <= 1000), indicating that there are n cities and m sars.
The following rows are three positive integers, startcity, endcity, and speed. startcity is converted to endcity on the surface, and the speed limit is speedsars. Speed <= 1000000
Then there is a positive integer Q (q <11), indicating the number of pathfinding.
Next, each row in row Q has two positive integers start and end, indicating the start and end of the path.
Output: print a line for each route seeking. Only one non-negative integer is output, indicating the maximum comfort speed and the lowest speed of the optimal route. If the start and end cannot be reached, the output is-1.
Sample Input
4 41 2 22 3 41 4 13 4 221 31 2
Sample output
10
Question: The minimum speed difference in the China Unicom Road.
Question: first sort edge weights from small to large, and then query the set enumeration.
#include <stdio.h>#include <string.h>#include <limits.h>#include <algorithm>#define maxn 202#define maxm 1002using std::sort;using std::min;int pre[maxn];struct Node{int u, v, cost;} E[maxm];int ufind(int k){int a = k, b;while(pre[k] != -1) k = pre[k];while(a != k){b = pre[a];pre[a] = k;a = b;}return k;}bool cmp(Node a, Node b){return a.cost < b.cost;}int main(){int n, m, q, a, b, i, j, x, y, ans;while(scanf("%d%d", &n, &m) == 2){for(i = 0; i < m; ++i)scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].cost);sort(E, E + m, cmp);scanf("%d", &q);while(q--){scanf("%d%d", &a, &b);ans = INT_MAX;for(i = 0; i < m; ++i){memset(pre, -1, sizeof(pre));for(j = i; j < m; ++j){x = ufind(E[j].u);y = ufind(E[j].v);if(x != y) pre[x] = y;if(ufind(a) == ufind(b)){ans = min(ans, E[j].cost - E[i].cost);break;}}}if(ans == INT_MAX) printf("-1\n");else printf("%d\n", ans);}}return 0;}