Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11964 Accepted Submission(s): 2786
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
Hint
Hint
Huge input, scanf recommended.Source
Recommend
题目大意:
输入n个节点,m条边,q个询问
接着输入i j k,表示i和j城市相连,路长为k
如果两个城市不能到达则输出Not connected,否则输出两个城市之间的距离
题解:
最近公共祖先+并查集
关键是需要把那些分开的树建立起关联,
所以弄个虚拟的0节点,把这些树的根都连在一起
#include<iostream> #include<vector> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> using namespace std; const int N=10005; const int M=25; int tot,cnt,n,m,q,s,t; int head[N],team[N]; int ver[2*N]; //ver:保存遍历的节点序列,长度为2n-1,从下标1开始保存 int R[2*N]; // R:和遍历序列对应的节点深度数组,长度为2n-1,从下标1开始保存 int first[N]; //first:每个节点在遍历序列中第一次出现的位置 int dir[N]; //dir:保存每个点到树根的距离,很多问题中树边都有权值,会询问两点间的距离,如果树边没权值,相当于权值为1 int dp[2*N][M]; struct edge { int u,v,w,next; }e[2*N]; void dfs(int u ,int fa,int dep) { ver[++tot]=u; R[tot] = dep; first[u]=tot; for(int k=head[u]; k!=-1; k=e[k].next) { int v=e[k].v, w=e[k].w; if (v==fa) continue; dir[v]=dir[u]+w; dfs(v,u,dep+1); ver[++tot]=u; R[tot]=dep; } } void ST(int n) { for(int i=0; i<=n; i++) dp[i][0] = i; for(int j=1; (1<<j)<=n; j++) { for(int i=0; i+(1<<j)-1<=n; i++) { int a = dp[i][j-1], b = dp[i+(1<<(j-1))][j-1]; dp[i][j] = R[a]<R[b]?a:b; } } } int RMQ(int l,int r) { int k=(int)(log((double)(r-l+1))/log(2.0)); int a = dp[l][k], b = dp[r-(1<<k)+1][k]; //保存的是编号 return R[a]<R[b]?a:b; } int LCA(int u ,int v) { int x = first[u] , y = first[v]; if(x > y) swap(x,y); int res = RMQ(x,y); return ver[res]; } void addedge(int u,int v,int w) { e[++cnt].u=u; e[cnt].v=v; e[cnt].w=w; e[cnt].next=head[u]; head[u]=cnt; } int findteam(int k) { if (team[k]==k) return k; else return team[k]=findteam(team[k]); } int main() { while(~scanf("%d%d%d",&n,&m,&q)) { memset(head,-1,sizeof(head)); for(int i=1;i<=n;i++) team[i]=i; //并查集 tot=0; cnt=0; for(int i=1;i<=m;i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); addedge(x,y,z); addedge(y,x,z); int fx=findteam(x); int fy=findteam(y); if (fx!=fy) team[fy]=fx; } for(int i=1;i<=n;i++) if (team[i]==i) //最关键的是给那些分开的树连一个0节点,那样就变成了一棵树 { addedge(i,0,0); addedge(0,i,0); } dir[0]=0; dfs(0,-1,1); ST(2*n-1); for(;q>0;q--) { scanf("%d%d",&s,&t); int croot=LCA(s,t); if (croot==0) printf("Not connected "); else printf("%d ",dir[s]+dir[t]-2*dir[croot]); } } return 0; }