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 Connected6HintHinthuge input, scanf recommended.
Source2009 multi-university Training Contest 8-host by Bjnu idea: use and check set to determine whether Unicom, because may not be connected, so may yo even many trees, DFS, and then is the template
#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<Set>#include<map>#defineTrue Ture#defineFalse flaseusing namespacestd;#definell Long Long#defineINF 0XFFFFFFFintScan () {intres =0, ch; while( ! (ch = getchar ()) >='0'&& CH <='9' ) ) { if(ch = = EOF)return 1<< - ; } Res= CH-'0' ; while(ch = getchar ()) >='0'&& CH <='9') Res= Res *Ten+ (CH-'0' ) ; returnRes;}#defineMAXN 100010#defineM 22intFATHER[MAXN];struct is{ intV,next,w;} EDGE[MAXN*2];intDeep[maxn],jiedge;intDIS[MAXN];intHEAD[MAXN];intFa[maxn][m];intFINDD (intx) { returnx==father[x]?x:father[x]=findd (father[x]);}intHebing (intUintv) { intx=findd (U); inty=findd (v); if(x!=y) father[x]=y;}voidAddintUintVintW) {Jiedge++; EDGE[JIEDGE].V=v; EDGE[JIEDGE].W=W; Edge[jiedge].next=Head[u]; Head[u]=Jiedge;}voidDfsintu) { for(intI=head[u]; I I=Edge[i].next) { intv=edge[i].v; intw=EDGE[I].W; if(!Deep[v]) {Dis[v]=dis[u]+EDGE[I].W; DEEP[V]=deep[u]+1; fa[v][0]=u; DFS (v); } }}voidStintN) { for(intj=1; j<m; J + +) for(intI=1; i<=n; i++) Fa[i][j]=fa[fa[i][j-1]][j-1];}intLCA (intU,intv) { if(Deep[u] <Deep[v]) Swap (U, v); intD = Deep[u]-Deep[v]; inti; for(i =0; i < M; i + +) { if( (1<< i) & D)//note here, try to simulate, you will understand{u=Fa[u][i]; } } if(U = = v)returnu; for(i = M-1; I >=0; I--) { if(Fa[u][i]! =Fa[v][i]) {u=Fa[u][i]; V=Fa[v][i]; }} u= fa[u][0] ; returnu;}voidInitintN) { for(intI=1; i<=n;i++) Father[i]=i; memset (Head,0,sizeof(head)); memset (FA,0,sizeof(FA)); memset (Deep,0,sizeof(deep)); Jiedge=0;}intMain () {intx,n,t; while(~SCANF ("%d%d%d",&n,&x,&t)) {init (n); for(intI=0; i<x; i++) { intu,v,w; scanf ("%d%d%d",&u,&v,&W); Add (U,V,W); Add (V,U,W); Hebing (U,V); } for(intI=1; i<=n;i++) if(!Deep[i]) {Deep[i]=1; Dis[i]=0; DFS (i); } st (n); while(t--) { intb; scanf ("%d%d",&a,&b); if(FINDD (a)! =findd (b)) printf ("Not connected\n"); Elseprintf ("%d\n", dis[a]-2*dis[lca (A, b)]+Dis[b]); } } return 0;}View Code
Hdu 2874 Connections between cities with right LCA