Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12894 Accepted Submission(s): 2977
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思路:如果有公共祖先就直接用深度差来求,如果没有公共祖先输出“Not connected”。代码:#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <stack> #include <map> using namespace std; const int inf=0x3f3f3f3f; const int M=2000005; const int N=10005; int head[N],h[N],dis[N],f[N],a[M/2],vis[N]; int cnt,num,tt; int n,m,z; struct node { int v,w,next; }e[N<<1],q[M]; void addedge(int u,int v,int w) { e[num].v=v; e[num].w=w; e[num].next=head[u]; head[u]=num++; e[num].v=u; e[num].w=w; e[num].next=head[v]; head[v]=num++; } void addque(int u,int v,int w) { q[tt].v=v; q[tt].w=w; q[tt].next=h[u]; h[u]=tt++; q[tt].v=u; q[tt].w=w; q[tt].next=h[v]; h[v]=tt++; } void init() { memset(head,-1,sizeof(head)); memset(h,-1,sizeof(h)); memset(vis,-1,sizeof(vis)); memset(f,-1,sizeof(f)); memset(dis,0,sizeof(dis)); memset(a,-1,sizeof(a)); cnt=num=tt=0; } int findx(int x) { if(x==f[x])return x; return f[x]=findx(f[x]); } void tarjan(int u,int w,int r) { dis[u]=w; f[u]=u; vis[u]=r; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].v; if(vis[v]==-1) { tarjan(v,w+e[i].w,r); f[v]=u; } } for(int i=h[u];i!=-1;i=q[i].next) { int v=q[i].v; if(vis[v]==r) { a[q[i].w]=dis[v]+dis[u]-2*dis[findx(v)]; } } } int main() { while(~scanf("%d%d%d",&n,&m,&z)) { init(); int i; int u,v,w; for(i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); } for(i=0;i<z;i++) { scanf("%d%d",&u,&v); addque(u,v,i); } for(i=1;i<=n;i++) { if(vis[i]==-1) { tarjan(i,0,i); } } for(i=0;i<z;i++) { if(a[i]!=-1) printf("%d\n",a[i]); else printf("Not connected\n"); } } return 0; }