N-Times minimum spanning tree Kruskal
Sort all the edges, and the weights are small in front.
After sorting the first edge as the longest edge in the path, the path must be made up of some edges in the 1~i
Because the highest speed and the most low-speed difference as small as possible, the highest speed determined that the minimum speed should be as large as possible.
So J from I Downto 1, the Edge J joins the Edge set, if s and T unicom at this time (S T in a set of and check set), then find a set of feasible solution: maximum I.W, minimum j.w, compared with the optimal solution, update.
If you are not connected, keep looking for the next J. Until J to 1 is not yet, then it is not possible to indicate that I is the largest side, i++ continues.
Code:
#include <iostream>#include<algorithm>#defineMAXN 505#defineMAXM 5005using namespacestd;intn,m;structedge{intU,v,w;} EG[MAXM];BOOLflag=false;intAnsa=0x3f3f3f3f, ansb=0;intStart,end;intPA[MAXN];voidinit () { for(intI=1; i<=n;i++) pa[i]=i;}intFindintx) { if(X!=pa[x]) pa[x]=find (pa[x]); returnpa[x];}voidUnintXinty) { if(Find (x) ==find (y))return; Pa[find (x)]=find (y);}BOOLff (Edge A,edge b) {returna.w<B.W;}intgcdintAintb) { returnB?GCD (b,a%b): A;}void out(intAintb) { if((Double) A/b < (Double) ansa/ANSB) { intt=gcd (A, b); Ansa=a/T; ANSB=b/T; //cout<<a<< "/" <<b<< "" <<ansa<< "/" <<ansb<<endl; }}intMain () {CIN>>n>>m; for(intI=1; i<=m;i++) {scanf ("%d%d%d",&eg[i].u,&eg[i].v,&EG[I].W); } CIN>>start>>end; Sort (eg+1, eg+1+m,ff); for(intI=1; i<=m;i++) {init (); for(intj=i;j>=1; j--) {un (EG[J].U,EG[J].V); if(Find (start) = =find (end)) { out(EG[I].W,EG[J].W); Flag=true; } } } if(flag) {cout<<Ansa; if(ansb!=1) cout<<'/'<<ANSB; } Elsecout<<"Impossible"; return 0;}
code1001 Comfortable Route