UPC 2784 Model Railroad

Source: Internet
Author: User
Tags gcd

Model Railroad time limit: 1 Sec memory limit: up to MB
Submitted by: 173 Resolution: 45
Submitted State [Discussion Version] [Propositional person:Admin] Title Description Since childhood you has been fascinated by model railroads. Designing your own tracks,complicated intersections, train stations with miniatures of travellers, train operators, Luggag E is so much fun! However, it also needs a lot of space. Since your house was more than full by now, you decide to move to the garden.
You had already moved all your completed tracks outside when you notice an important? AW:
Since different tracks were in different rooms before, there is stations which cannot is reached from all other. that have to change!
Since you have already? Xed the exact positions of the stations, you know the lengths for all possible connections you can Build and also which stations is connected already. All connections can is used in both directions. You can decide to remove some existing connections and instead build new ones of in most of the same total length. Is it possible to rearrange the railroads so, every station was reachable from all other stations?

Enter the input consists of:
one line with three integers n (2≤n≤5 104), M (0≤m≤2.5 105) and L (0≤l≤m), where n is the number of STA tions, M is the number of possible connections and L are the number of connections already built;
• m lines describing the connections. Each connection are described by:
–one line with three integers a, B (1≤a, b≤n), and C (0≤c≤5 103) describing, there are a connection from St Ation A to station B of length C.
The. Rst L of those connections already exist.

Output "Possible" if it is possible to construct a connected network as described above,otherwise output "impossible".

Sample input
4 6 31 2 12 1 23 4 21 3 21 4 32 4 2

Sample output
Possible

Tips

Figure E.1 depicts the "RST" sample case. It is possible to connect all stations by removing the connections between stations 1 and 2 of length 2 and instead Buildi Ng the connection between stations 2 and 4. The curvature of the rails does not matter because a hammer. In the second case, depicted in figure e.2, it's not possible to connect all three stations.

Test instructions

N points, M-bar, where the front I edge has been built, the length of each side is known, to build a not built side must be a shorter than the length of a built edge. Finally, each point is connected to each other.

Analysis

Run one side of the minimum spanning tree, determine whether the cost of the minimum spanning tree is less than the cost of the front I, and finally judge whether the link, that is, to see the minimum spanning tree is not n-1 edge on the line.

///Author:kissheart///#include <stdio.h>#include<algorithm>#include<iostream>#include<string.h>#include<vector>#include<stdlib.h>#include<math.h>#include<queue>#include<deque>#include<ctype.h>#include<map>#include<Set>#include<stack>#include<string>#defineINF 0x3f3f3f3f#defineFast_io Ios::sync_with_stdio (False)Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-6;Const intmax=3e5+Ten;Const intmod=1e9+7; typedefLong Longll;using namespacestd;#defineGCD (A, B) __gcd (A, B)Inline ll LCM (ll A,ll b) {returnA/GCD (A, b) *b;} inline ll Qpow (ll A,ll b) {ll r=1, T=a; while(b) {if(b&1) r= (r*t)%mod;b>>=1; t= (t*t)%mod;}returnR;} inline ll Inv1 (ll b) {returnQpow (b,mod-2);} inline ll EXGCD (ll A,ll b,ll&x,ll &y) {if(!B) {x=1; y=0;returnA;} ll R=EXGCD (b,a%b,y,x); y-= (A/b) *x;returnR;} inline ll read () {ll x=0, f=1;CharC=getchar (); for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1; for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0';returnx*F;}//freopen ("In.txt", "R", stdin);//freopen ("Data.txt", "w", stdout);structedge{intx,y,w; Edge (intx=0,inty=0,intw=0): X (x), Y (y), W (w) {}}e[max];intfa[max],n,m;intp,sum1,sum2;intGetfather (intx) {    if(X==fa[x])returnx; Else returnfa[x]=Getfather (fa[x]);}intcmp (Edge A,edge b) {returna.w<B.W;}intCur=0;intkruscal () {intans=0; Sort (e+1, e+1+m,cmp); intCnt=N;  for(intI=1; i<=n;i++) fa[i]=i;  for(intI=1; i<=m;i++)    {        intt1=Getfather (e[i].x); intT2=Getfather (E[I].Y); if(t1!=T2) {FA[T1]=T2; Ans+=E[I].W; Cur++; if(cnt==1) Break; }    }    returnans;}intMain () {scanf ("%d%d%d",&n,&m,&p);  for(intI=1; i<=m;i++) {scanf ("%d%d%d",&e[i].x,&e[i].y,&E[I].W); if(i<=p) sum1+=E[I].W; } sum2=kruscal (); if(cur!=n-1) {printf ("impossible\n"); return 0; }        if(sum2<=sum1) printf ("possible\n"); Elseprintf ("impossible\n"); return 0;}
View Code

UPC 2784 Model Railroad

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.