Description
The Town of Z is a pleasant place to visit, attracting tourists from all over the world.
A total of z small town
N (1<n≤500) Attractions (numbered,..., N), these attractions are connected by M (0<m≤5000) road, all roads are bidirectional, and there may be many roads between the two attractions. Perhaps to protect the tourist resources of the land, Z town has a strange rule that for a given road RI, any vehicle on that road must be at a speed of VI. Frequent changes in speed make visitors uncomfortable, so when you go from one spot to another, you want to choose a route that is as small as the maximum speed and minimum speed in the process, the so-called most comfortable route.
Input
The first line contains two positive integers, N and M.
The next M-line contains three positive integers per line: x, Y and V (the last line of 1≤x,y≤n,0 contains two positive integer s,t, indicating the shortest path from attraction s to the maximum minimum speed of attraction T. S and T cannot be the same.
Output
If the attraction s to attractions t have no path, output "impossible". Otherwise the output is a number that represents the minimum speed ratio. If desired, output a minimal fraction.
Sample Input
Example 1
4 2
1 2 1
3 4 2
1 4
Example 2
3 3
1 2
1 2 5
2 3 8
1 3
Example 3
3 2
1 2 2
2 3 4
1 3
Sample Output
Example 1
Impossible
Example 2
5/4
Example 3
2
Hint
N (1<n≤500) M (0<m≤5000) vi within the INT range
Ideas
The idea of minimum spanning tree Kruskal, according to the speed from small to large, from the lowest speed of the road to enumerate, the two ends of each road to the same set, to determine whether the starting point can reach the end point, update the minimum value. Another similar question: Point me
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int MAXN = 5005;const int INF = 0x3f3f3f3f;int n,m,rk[maxn],father[maxn];struct edge{int u,v,w;} Edge[maxn];bool cmp (struct edge x,struct edge y) {return x.w < Y.W;} void Init () {memset (rk,0,sizeof (RK)); Memset (father,0,sizeof (father)); for (int i = 0;i <= n;i++) {Father[i] = i;}} int find (int x) {int r = x;while (r! = Father[r]) {r = Father[r];} int i = X,j;while (i! = r) {j = father[i];father[i] = R;i = j;} return r;} void Unite (int x,int y) {x = find (x), y = Find (Y), if (x! = y) {if (Rk[x] < Rk[y]) {father[x] = y;} Else{father[y] = x;if (rk[x] = = Rk[y]) {rk[x]++;}}} int gcd (int a,int b) {return B?GCD (b,a%b): A;} int main () {while (~scanf ("%d%d", &n,&m)) {int s,t,fz,fm;double res = inf;for (int i = 0;i < m;i++) {scanf ("%d%d%d" , &EDGE[I].U,&EDGE[I].V,&EDGE[I].W);} scanf ("%d%d", &s,&t); sort (Edge,edge + m,cmp); for (int i = 0;i < m;i++) {init (); for (int j =I;j < m;j++) {unite (EDGE[J].U,EDGE[J].V); if (find (s) = = Find (t)) {Double TMP = 1.0*EDGE[J].W/EDGE[I].W;IF (tmp < RES) {res = TMP;FZ = EDGE[J].W;FM = EDGE[I].W;}}} int com = GCD (FZ,FM), res = = inf?printf ("impossible\n"): fm/com = = 1?printf ("%d\n", fz/com):p rintf ("%d/%d\n", fz/com,fm/ COM);} return 0;}
Codevs comfortable route (Kruskal)