Connections between cities

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 896 Accepted Submission(s): 236
 
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.
 
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
  1. 5 3 2
  2. 1 3 2
  3. 2 4 3
  4. 5 2 3
  5. 1 4
  6. 4 5
 
Sample Output
  1. Not connected
  2. 6
Hint

Hint Huge input, scanf recommended.

 
 
Source
2009 Multi-University Training Contest 8 - Host by BJNU
 
Recommend
gaojie
 
  1. /*
  2. 用vector炸了,还是用邻接表卡过的
  3. */
  4. #include<bits/stdc++.h>
    using namespace std;
  5. #define M 10007
  6. #define N 2222212
  7. int bin[M],dis[M],vis[M],cur[N],root[M];
  8. int s1[N],s2[N],t[N],d[N],p[N];
  9. int n,m,ne,cnt;
  10.  
  11. int find(int x)
  12. {
  13. while(x!=bin[x])
  14. x=bin[x];
  15. return x;
  16. }
  17.  
  18. void add(int u,int v,int w,int h[])
  19. {
  20. t[ne]=v,d[ne]=w,p[ne]=h[u],h[u]=ne++;
  21. t[ne]=u,d[ne]=w,p[ne]=h[v],h[v]=ne++;
  22. }
  23. /*
  24. LCA算法以某一个节点作为根节点,开始遍历,遍历到一个结点先判断与它相关的结点是不是有已经被访问过的,
  25. 如果有的话,判断两个结点是不是在同一棵树上,如果是的话就保留最近公共祖先的距离,如果不是的话,就是不能到达,距离就赋值为-1
  26.  
  27. */
  28. void LCA(int u)
  29. {
  30. root[u]=cnt;
  31. bin[u]=u;
  32. vis[u]=;
  33. //遍历查询树
  34. for(int i=s2[u];i;i=p[i])
  35. {
  36. int v=t[i];
  37. if(vis[v])
  38. {
  39. if(root[u]==root[v])//在同一棵树下
  40. {
  41. int rt=find(v);//最近公共祖先
  42. cur[d[i]]=dis[u]+dis[v]-*dis[rt];
  43. }
  44. else
  45. cur[d[i]]=-;
  46. }
  47.  
  48. }
  49. //遍历城市图
  50. for(int i=s1[u];i;i=p[i])
  51. {
  52. int v=t[i];
  53. if(!vis[v])
  54. {
  55. dis[v]=dis[u]+d[i];
  56. LCA(v);
  57. bin[v]=u;//路径压缩
  58. }
  59. }
  60. }
  61.  
  62. int main()
  63. {
  64. //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
  65. int n,m,q,i,j;
  66. while(scanf("%d%d%d",&n,&m,&q)!=EOF)
  67. {
  68. for(i=,ne=;i<=n;i++)
  69. {
  70. s1[i]=s2[i]=vis[i]=cur[i]=root[i]=bin[i]=;
  71. }
  72. int u,v,w;
  73. for(i=;i<=m;i++)
  74. {
  75. scanf("%d%d%d",&u,&v,&w);
  76. add(u,v,w,s1);
  77. }
  78. for(i=;i<=q;i++)
  79. {
  80. scanf("%d%d",&u,&v);
  81. add(u,v,i,s2);
  82. }
  83. for(i=,cnt=;i<=n;i++,cnt++)
  84. if(!vis[i])
  85. {
  86. dis[i]=;
  87. LCA(i);
  88. }
  89.  
  90. for(i=;i<=q;i++)
  91. if(cur[i]>=)
  92. printf("%d\n",cur[i]);
  93. else
  94. printf("Not connected\n");
  95. }
  96. return ;
  97. }
  1. #include<bits/stdc++.h>
  2. #define N 10005
  3. using namespace std;
  4. int n,m,c;
  5. int bin[N];
  6. int root[N];//表示点i的根节点
  7. int roo;//表示当前是以哪个结点为根节点遍历的
  8. int dis[N];//标记结点i到根结点的距离
  9. int vis[N];//标记i点是否被访问过
  10. int cur[N];//表示第几组解
  11. int op[N][N];//表示解是第几组
  12. struct node
  13. {
  14. int v,val;
  15. node(){}
  16. node(int a,int b)
  17. {
  18. v=a;
  19. val=b;
  20. }
  21. };
  22. vector<node>edge[N];
  23. vector<node>edg[N];
  24. int findx(int x)
  25. {
  26. while(x!=bin[x])
  27. x=bin[x];
  28. return x;
  29. }
  30. /*
  31. Tarjan算法以某一个节点作为根节点,开始遍历,遍历到一个结点先判断与它相关的结点是不是有已经被访问过的,
  32. 如果有的话,判断两个结点是不是在同一棵树上,如果是的话就保留最近公共祖先的距离,如果不是的话,就是不能到达,距离就赋值为-1
  33.  
  34. */
  35. int LCA(int u)
  36. {
  37. //首先遍历查询树
  38. root[u]=roo;
  39. vis[u]=;
  40. bin[u]=u;
  41. for(int i=;i<edg[u].size();i++)
  42. {
  43. int nex=edg[u][i].v;
  44. if(vis[nex]==)//这个点遍历过了
  45. {
  46. if(root[u]==root[nex])
  47. {
  48. int rt=findx(nex);
  49. cur[op[u][nex]]=dis[u]+dis[nex]-*dis[rt];//
  50. }
  51. else
  52. cur[op[u][nex]]=-;
  53. }
  54. }
  55. //然后就是遍历程城市树
  56. for(int i=;i<edge[u].size();i++)
  57. {
  58. int nex=edge[u][i].v;
  59. if(vis[nex]) continue;
  60. dis[nex]=dis[u]+edge[u][i].val;//父节点到根节点的距离,加上到父节点的距离
  61. LCA(nex);
  62. bin[nex]=u;//路径压缩
  63. }
  64. }
  65. int x,y,val;
  66. void inti()
  67. {
  68. for(int i=;i<=n;i++)
  69. {
  70. edge[i].clear();
  71. edg[i].clear();
  72. bin[i]=i;
  73. cur[i]=-;
  74. dis[i]=;
  75. vis[i]=;
  76. }
  77. }
  78. int main()
  79. {
  80. //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
  81. while(scanf("%d%d%d",&n,&m,&c)!=EOF)
  82. {
  83. inti();
  84. //构建城市的图
  85. for(int i=;i<m;i++)
  86. {
  87. scanf("%d%d%d",&x,&y,&val);
  88. edge[x].push_back(node(y,val));
  89. edge[y].push_back(node(x,val));
  90. }
  91. //构建查询树
  92. for(int i=;i<c;i++)
  93. {
  94. scanf("%d%d",&x,&y);
  95. op[x][y]=op[y][x]=i;
  96. edg[x].push_back(node(y,));
  97. edg[y].push_back(node(x,));
  98. }
  99. for(int i=;i<=n;i++)
  100. {
  101. if(!vis[i])
  102. {
  103. roo=i;
  104. dis[i]=;
  105. LCA(i);
  106. }
  107. }
  108. for(int i=;i<c;i++)
  109. {
  110. if(cur[i]==-)
  111. puts("Not connected");
  112. else
  113. printf("%d\n",cur[i]);
  114. }
  115. }
  116. return ;
  117. }

Connections between cities的更多相关文章

  1. hdu 2874 Connections between cities [LCA] (lca->rmq)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  2. HDU 2874 Connections between cities(LCA Tarjan)

    Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...

  3. hdu 2874 Connections between cities 带权lca判是否联通

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  4. hdu 2874 Connections between cities(st&rmq LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  5. hdu 2874 Connections between cities (并查集+LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  6. hdu-2874 Connections between cities(lca+tarjan+并查集)

    题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 32768/327 ...

  7. HDU 2874 Connections between cities(LCA)

    题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...

  8. HDU——2874 Connections between cities

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  9. Connections between cities LCA

    Problem Description After World War X, a lot of cities have been seriously damaged, and we need to r ...

随机推荐

  1. angularui 自定义选项卡

    ng-include 选取ng-template <!DOCTYPE html> <html lang="en" ng-app="myApp" ...

  2. 乐橙谷浅析JAVA程序员就业前景

    不知道大家对Java就业前景了解多少.随着信息化的发展,IT培训受倒了越来越多人的追捧.在开发领域,JAVA培训成为了许多人的首选!JAVA应用广泛,JAVA培训就业前景良好!目前,虽然JAVA人才的 ...

  3. Android 8.0 功能和 API

    Android 8.0 为用户和开发者引入多种新功能.本文重点介绍面向开发者的新功能. 用户体验 通知 在 Android 8.0 中,我们已重新设计通知,以便为管理通知行为和设置提供更轻松和更统一的 ...

  4. epoll的ET和LT两种模式对比及注意事项

    ET模式: 因为ET模式只有从unavailable到available才会触发,所以 1.读事件:需要使用while循环读取完,一般是读到EAGAIN,也可以读到返回值小于缓冲区大小: 如果应用层读 ...

  5. Maven仓库搜索jar包依赖网址

    可在该网站搜索jar包依赖 http://search.maven.org/

  6. 第一次安装jshint,jshint新手使用记录

    刚刚出来工作的渣渣,第一次进入这样比较正规的公司,各个开发流程都比较严格,代码也是要经过jshint的检测才能上传到svn才能成功打包项目.所以我这种技术都半桶水的职场开发小白,也是第一次用jshin ...

  7. POJ-1273-Drainage Ditches(网络流之最大流)

    Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This ...

  8. .Net 内存对象分析

    在生产环境中,通过运行日志我们会发现一些异常问题,此时,我们不能直接拿VS远程到服务器上调试,同时日志输出的信息无法百分百反映内存中对象的状态,比如说我们想查看进程中所有的Socket连接状态.服务路 ...

  9. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)

    http://codeforces.com/contest/831 A. Unimodal Array time limit per test 1 second memory limit per te ...

  10. Saturday SQL Server 2016 初体验

    最近在开发一个有关数据库的项目,我想用SQLite,但是SQLite的设计器不是特别友好,然后据说VS有一个集成的SQLite设计器,但是我用的VS2017亲测并没有,用户体验不佳,所以安装一个SQL ...