HDU 2874 最近公共祖先lca
Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10094 Accepted Submission(s): 2419
Problem Description
After
World War X, a lot of cities have been seriously damaged, and we need
to rebuild those cities. However, some materials needed can only be
produced in certain places. So we need to transport these materials from
city to city. For most of roads had been totally destroyed during the
war, there might be no path between two cities, no circle exists as
well.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
Input
Input
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 has three integers i, j and k,
represent a road between city i and city j, with length k. Last c lines,
two integers i, j each line, indicates a query of city i and city j.
Output
For
each problem instance, one line for each query. If no path between two
cities, output “Not connected”, otherwise output the length of the
shortest path between them.
Sample Input
5 3 2
1 3 2
2 4 3
5 2 3
1 4
4 5
Sample Output
Not connected
6
1 #include <iostream> 2 #include <cstring> 3 #define N 10002 4 #define M 1000002 5 using namespace std; 6 int n,m,c; 7 int head[N],qhead[N],vis[N],ans[M],dis[N]; 8 struct node{ 9 int to,next,lca; 10 }; 11 node edge[N*2],qedge[M*2]; 12 int id,qid; 13 void add(int x,int y,int z){ 14 edge[id].to=y; 15 edge[id].lca=z; 16 edge[id].next=head[x]; 17 head[x]=id++; 18 edge[id].to=x; 19 edge[id].lca=z; 20 edge[id].next=head[y]; 21 head[y]=id++; 22 } 23 24 void qadd(int x,int y,int z){ 25 qedge[qid].to=y; 26 qedge[qid].lca=z; 27 qedge[qid].next=qhead[x]; 28 qhead[x]=qid++; 29 qedge[qid].to=x; 30 qedge[qid].lca=z; 31 qedge[qid].next=qhead[y]; 32 qhead[y]=qid++; 33 } 34 35 int p[N]; 36 int find(int x){ 37 if(p[x]==x){ 38 return x; 39 } 40 return p[x]=find(p[x]); 41 } 42 43 void lca(int k,int deep,int root){ 44 int j,v; 45 p[k]=k; 46 vis[k]=root; 47 dis[k]=deep; 48 for(j=head[k];j!=-1;j=edge[j].next){ 49 v=edge[j].to; 50 if(vis[v]!=-1){ 51 continue; 52 } 53 lca(v,deep+edge[j].lca,root); 54 p[v]=k; 55 } 56 for(j=qhead[k];j!=-1;j=qedge[j].next){ 57 v=qedge[j].to; 58 if(vis[v]!=root){ 59 continue; 60 } 61 ans[qedge[j].lca]=dis[v]+dis[k]-2*dis[find(v)];//重点:两点到根的距离之和减去两点的祖先节点到根节点的距离的两倍 62 } 63 } 64 65 int main(){ 66 cin.sync_with_stdio(false); 67 int x,y,z; 68 while(cin>>n>>m>>c){ 69 id=0; 70 qid=0; 71 memset(head,-1,sizeof(head)); 72 memset(qhead,-1,sizeof(qhead)); 73 while(m--){ 74 cin>>x>>y>>z; 75 add(x,y,z); 76 } 77 for(int i=0;i<c;i++){ 78 cin>>x>>y; 79 ans[i]=-1; 80 qadd(x,y,i); 81 } 82 memset(vis,-1,sizeof(vis)); 83 for(int i=1;i<=n;i++){ 84 if(vis[i]==-1){ 85 lca(i,0,i); 86 } 87 } 88 for(int i=0;i<c;i++){ 89 if(ans[i]==-1){ 90 cout<<"Not connected"<<endl; 91 } 92 else{ 93 cout<<ans[i]<<endl; 94 } 95 } 96 } 97 return 0; 98 }
2016-12-05 16:07:48