CODEVS1001 Comfortable route (and check set)

Source: Internet
Author: User

For all edges from large to small sort, enumerate the maximum edges, O (m) Verify, use and check the set maintenance diagram for connectivity.

 ProgramCODEVS1001;Constmaxm=5008; MAXN=508; INF=2000000000;typeArr=RecordU,v,w:int64; End;varegArray[0.. MAXM] ofarr; FA:Array[0.. MAXN] ofLongint;        I,j,m,n,x,y,z,s,t,u,v,d:longint; Ans1,ans2:int64;procedureAdd (u,v,w:longint);beginInc (J); EG[J].U:=u; EG[J].V:=v; EG[J].W:=W;End; proceduresort (l,r:longint); varI,j,x:longint;      Y:arr; beginI:=l; J:=R; X:=eg[(L+r)Div 2].W; Repeat            whileEg[i].w>x DoInc (I);  whileX>eg[j].w DoDec (j); if  not(I&GT;J) Then             beginy:=Eg[i]; Eg[i]:=Eg[j]; EG[J]:=y;                Inc (I); J:=j-1; End; untilI>J; ifL<j Thensort (l,j); ifI<r Thensort (i,r); End;functionFind (X:longint): Longint;begin  ifFa[x]=x Thenexit (x); FA[X]:=find (fa[x]); Exit (Fa[x]);End;functiongcd (x,y:longint): Longint;begin  ify=0  ThenExit (x)ElseExit (GCD (y,xMoDy));End;beginreadln (n,m); J:=0;  fori:=1  toM Do    beginreadln (x, y, z);    Add (x, y, z); End;  READLN (s,t); Sort (1, M); ANS1:=INF; ANS2:=-INF;  fori:=1  toM Do    begin       forj:=1  toN Dofa[j]:=J;  forJ:=i toM Do        beginu:=eg[j].u; v:=eg[j].v; X:=find (U); Y:=Find (v); ifX<>y Then            beginFa[x]:=y; ifFind (Fa[s]) =find (Fa[t]) Then                begin                  ifEg[i].w*ans2<eg[j].w*ans1 Then                    beginans1:=EG[I].W; ANS2:=EG[J].W; End;                Continue End; End; End; End; D:=gcd (ANS1,ANS2); ifAns1=inf ThenWriteln ('Impossible')Else    ifD=ans2 ThenWriteln (ans1DivANS2)ElseWriteln (ans1DivD'/', Ans2Divd);End.

CODEVS1001 Comfortable route (and check set)

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.