[关闭]
@Chilling 2017-02-16T17:51:37.000000Z 字数 2788 阅读 1013

HDU-2874: Connections between cities(LCA+并查集)

LCA


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.

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

Huge input, scanf recommended.

题意:输入n,m,c,表示有n个城市(编号1到n),之间有m条路,c次询问。输入m行表示x城市到y城市之间的距离为z,c次询问x城市到y城市的距离是多少,若两个城市不连通则输出Not connected。

分析:由于n的范围是10000,c的范围是一百万,因此不能用dij在每次询问之后求最短路,会超时。
求两点之间的距离可以用LCA,但是这道题给出的是森林,而不是树,因此我们首先使用并查集处理,判断节点是否在同一棵树上,然后再将根节点与虚节点(0)连接起来,使之构成一棵树,再利用LCA求解。


  1. #include<stdio.h>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<string.h>
  5. const int maxn=10005;
  6. using namespace std;
  7. int n,m,c;
  8. int father[maxn];
  9. int dep[maxn],dis[maxn];
  10. int fa[maxn][15];
  11. struct node
  12. {
  13. int en,val;
  14. node(int enn=0,int vall=0)
  15. {
  16. en=enn;
  17. val=vall;
  18. }
  19. };
  20. vector<node> V[maxn];
  21. void clr()
  22. {
  23. memset(dep,0,sizeof(dep));
  24. memset(dis,0,sizeof(dis));
  25. memset(fa,0,sizeof(fa));
  26. for(int i=0;i<maxn;i++)
  27. father[i]=i;
  28. for(int i=0;i<=n;i++)
  29. V[i].clear();
  30. }
  31. int findx(int x)//并查集
  32. {
  33. if(x!=father[x])
  34. father[x]=findx(father[x]);
  35. return father[x];
  36. }
  37. void merge(int a,int b)//将father统一为树上最小一点
  38. {
  39. int x=findx(a);
  40. int y=findx(b);
  41. if(x>y)
  42. father[x]=y;
  43. else
  44. father[y]=x;
  45. }
  46. void dfs(int u,int f,int d)
  47. {
  48. fa[u][0]=f;
  49. for(int i=1;i<15;i++)
  50. fa[u][i]=fa[fa[u][i-1]][i-1];
  51. dep[u]=d;
  52. for(int i=0;i<V[u].size();i++)
  53. {
  54. node v=V[u][i];
  55. if(v.en==f) continue;
  56. dis[v.en]=dis[u]+v.val;
  57. dfs(v.en,u,d+1);
  58. }
  59. }
  60. int LCA(int u,int v)
  61. {
  62. if(dep[u]>dep[v])
  63. swap(u,v);
  64. for(int i=0;i<15;i++)
  65. if((dep[v]-dep[u])>>i&1)
  66. v=fa[v][i];
  67. if(u==v)
  68. return u;
  69. for(int i=14;i>=0;i--)
  70. {
  71. if(fa[u][i]!=fa[v][i])
  72. {
  73. u=fa[u][i];
  74. v=fa[v][i];
  75. }
  76. }
  77. return fa[u][0];
  78. }
  79. void solve()
  80. {
  81. for(int i=1;i<=n;i++)//若为根节点,则连接到序号为0的虚根上
  82. if(father[i]==i)
  83. {
  84. V[i].push_back(node(0,0));
  85. V[0].push_back(node(i,0));
  86. }
  87. dfs(0,-1,1);//节点为0,父节点-1,深度为1
  88. }
  89. int main()
  90. {
  91. int x,y,z;
  92. while(scanf("%d%d%d",&n,&m,&c)!=EOF)
  93. {
  94. clr();
  95. while(m--)
  96. {
  97. scanf("%d%d%d",&x,&y,&z);
  98. merge(x,y);
  99. V[x].push_back(node(y,z));
  100. V[y].push_back(node(x,z));
  101. }
  102. solve();
  103. while(c--)
  104. {
  105. scanf("%d%d",&x,&y);
  106. if(findx(x)!=findx(y))//不在同一棵树上
  107. printf("Not connected\n");
  108. else
  109. printf("%d\n",dis[x]+dis[y]-2*dis[LCA(x,y)]);
  110. }
  111. }
  112. return 0;
  113. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注