The topics are as follows:
Title Description
Give you an n<=500 graph, N (a) vertex, M (m<=5000) edge, each edge has a weight Vi (vi<30000). Give you two vertices s and t to find a path that minimizes the ratio of the maximum and minimum edges on the path. If there is no path between S and T, the output "impossible", otherwise the output of this ratio, if necessary, expressed as an approximate fraction. Note: There may be more than one path between two vertices.
input
The first line contains two positive integers, N and M. The M line down contains three positive integers per line: x, Y and V. Indicates that there is a two-way road between the sights X and the attraction Y, and the vehicle must travel at speed v on the public road. The last line 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 necessary, output an approximate fraction.
Sample Input
"Sample Input 1"
4 2
1 2 1
3 4 2
1 4
"Sample Input 2"
3 3
1 2 10
1 2 5
2 3 8
1 3
"Sample Input 3"
3 2
1 2 2
2 3 4
1 3
Sample Output
"Sample Output 1"
Impossible
"Sample Output 2"
5/4
"Sample Output 3"
2
Using structural body: e[].fr (starting point) e[].to (point) e[].cost (weight value)
This problem is done with a set of checks. First, all edges are sorted from small to large, defining ans1=inf as the minimum value for the largest edge currently found, and the ans2=1 bit current minimum edge. Then from small to large enumeration (loop one: for(i=1;i<=m;i++)) in the loop one to establish a point as the keyword and a collection array. Then the second loop (forint j=i;j<=n;j++), which starts with the number of the edge from the loop one to the largest edge, adds each edge to the top and the end of the loop, Re-detect the connectivity of S and T (if (Find (S) ==find (T)) {}), and if Unicom compares ans1/ans2 with e[J].cost/e[I].cost the size of the comparison process can be cross-multiplied, This avoids the problem of dividing evenly. If the new ratio is smaller, update the values of ans1 and ans2 and jump out of loop two. If the non-connected still compare ans1/ans2 and e[J].cost/e[I].cost size, if the new ratio is less than the original deposit ratio, then jump out of the loop two, because the loop of the two e[j].cost is increasing. At the end of the cycle, the subject ends, and the final output is understood.
#include <iostream>
#include
<cstdio>
#include
<algorithm> #define inf 10000000
using namespacestd;structedge{intFr,to,cost;}; Edge e[5001];inlineintComp (Edge A,edge b) {returna.cost<B.cost;}intm,n,s,t;intfa[501];inlineintFindintx) { if(fa[x]!=x) fa[x]=find (fa[x]); returnfa[x];} InlineintgcdintAintb//a>b{ if(b==0)returnA; returnGCD (b,a%b);}intMain () {scanf ("%d%d",&n,&m); for(intI=1; i<=m;i++) scanf ("%d%d%d",&e[i].fr,&e[i].to,&e[i].cost); scanf ("%d%d",&s,&t); Sort (E+1, e+m+1, comp); intAns1=inf,ans2=1; for(intI=1; i<=m;i++){ for(intj=1; j<=n;j++) Fa[j]=J; for(intj=i;j<=m;j++){ intF1=find (e[j].fr), f2=find (e[j].to); FA[F1]=F2; if(Find (s) = =find (t)) { if(e[i].cost*ans1>e[j].cost*ans2) ans1=e[j].cost,ans2=E[i].cost; Break; } if(E[I].COST*ANS1<E[J].COST*ANS2) Break; } } intzdg=gcd (ANS1,ANS2); if(Ans1==inf) printf ("Impossible"); Else if(ANS2!=ZDG) printf ("%d/%d", ans1/zdg,ans2/ZDG); Elseprintf"%d", ans1/ans2);}
BZOJ 1050 Travel COMF