hdu-2874 Connections between cities (lca+tarjan+ and collection)

Source: Internet
Author: User

Topic Links:

Connections between cities

Time limit:10000/5000 MS (java/others)

Memory limit:32768/32768 K (java/others)


problem DescriptionAfter World War X, a lot of cities has been seriously damaged, and we need to rebuild those cities. However, some materials needed can only is produced in certain places. So we need the transport these materials from the city. For very roads had been totally destroyed during the war, there might be no path between both cities, no circle exists a s well.
Now, your task comes. After giving your condition of the roads, we want to know if there exists a path between any and both cities. If The answer is yes, output the shortest path between them.

InputInput consists of multiple problem instances. For each instance, first line contains three integers n, m and C, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000 . n represents the number of cities numbered from 1 to N. Following m lines, each line had three integers I, j and K, represent a road between City I and City J, with length K. Las t c lines, integers i, j each line, indicates a query of the city I and the City J.

OutputFor each problem instance, one line for each query. If no path between the cities, output "not connected", otherwise output the length of the shortest path between them.

Sample Input5 3 21 3 22 4 35 2 31 44 5

Sample OutputNot Connected6Test Instructions:Gave some side, could be a tree, could be a forest, a C inquiry, a distance between the I and J;Ideas:Because it is a forest, the off-line algorithm of LCA is Tarjan; open two and check set, one can be pre-pretreated whether in a tree, another used on Tarjan; Tarjan the idea of the algorithm is to deal with one side, while answering the query, a node of all the sub-tree processing can be completed on the node and all the processing point of the inquiry;AC Code:
/*2874 1731MS 29448K 2286 B g++ 2014300227*/#include<bits/stdc++.h>using namespacestd;Const intn=1e4+4; typedefLong Longll;Const DoublePi=acos (-1.0);intn,m,c,cnt,head[n],a,b,num,pre[n],p[n],l,r,v,dis[n],ans[1000005],vis[n],fa[n];structedge{intTo,va,next;}; Edge edge[2*N];structques{intTo,next,id;}; Ques que[2000000+ -];voidAddintSintEintv) {    //edge[cnt].fr = s;Edge[cnt].to =e; Edge[cnt].va=v; Edge[cnt].next=Head[s]; Head[s]= cnt++;//learned the method of saving the edge;}voidQ_add (intSintEintorder) {    //que[num].fr = s;Que[num].to =e; Que[num].next=Pre[s]; Que[num].id=order; Pre[s]= num++;}intFindset (intx) {    if(x = = P[x])returnx; returnP[X] =Findset (p[x]);}intFunintx) {    if(X==fa[x])returnx; returnfa[x]=Fun (fa[x]);}voidTarjan (intXintDist) {Vis[x]=1; DIS[X]=Dist;  for(inti = head[x];i!=-1; i =Edge[i].next)//head[x] points to an edge with x as the endpoint; the pre[x below] is the same; {        inty =edge[i].to; if(!Vis[y]) {Tarjan (y,dist+Edge[i].va); Fa[y]=x;    }}//front indicates that all the subtree of this node has been processed, the following can answer the relevant inquiries;  for(inti = pre[x];i!=-1; i =Que[i].next) {        inty =que[i].to; if(Findset (x) = =Findset (y)) {            if(Vis[y]) ans[que[i].id]= dis[y]+dis[x]-2*Dis[fun (y)]; }        ElseAns[que[i].id] =-1; }}intMain () { while(SCANF ("%d%d%d", &n,&m,&c)! =EOF) {         for(inti =0; I <= n;i++) {P[i]=i; Fa[i]=i; Head[i]=pre[i]=-1; Vis[i]=0; } CNT=0; Num=0;  for(inti =0; I < m;i++) {scanf ("%d%d%d",&l,&r,&v); intFx=findset (l), fy=Findset (R); if(fx!=fy) p[fx]=fy;//See if it is on a tree and check set Add (L,R,V);        Add (R,L,V); }         for(inti =0; I < c;i++) {scanf ("%d%d",&a,&b);            Q_add (A,b,i);        Q_add (B,a,i); }         for(intI=1; i<=n;i++)        {            if(!Vis[i]) {Tarjan (i,0); }        }         for(inti =0; I < c;i++)        {            if(ans[i]>-1) printf ("%d\n", Ans[i]); Elseprintf"Not connected\n"); }    }    return 0;}

hdu-2874 Connections between cities (lca+tarjan+ and collection)

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.