code1001 Comfortable Route

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.