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

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.

  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <memory.h>
  7. #include<vector>
  8. using namespace std;
  9. int Laxt[],Next[],To[],Len[];
  10. int Laxt2[],Next2[],To2[],ans[];
  11. bool vis[];
  12. int cnt,cnt2;
  13. int dis[],fa[];
  14. void _update()
  15. {
  16. memset(Laxt,-,sizeof(Laxt));
  17. memset(Laxt2,-,sizeof(Laxt2));
  18. memset(vis,false,sizeof(vis));
  19. cnt=cnt2=;
  20. }
  21. void _add(int u,int v,int d){
  22. Next[cnt]=Laxt[u];
  23. Laxt[u]=cnt;
  24. To[cnt]=v;
  25. Len[cnt++]=d;
  26. }
  27. void _add2(int u,int v){
  28. Next2[cnt2]=Laxt2[u];
  29. Laxt2[u]=cnt2;
  30. To2[cnt2++]=v;
  31. Next2[cnt2]=Laxt2[v];
  32. Laxt2[v]=cnt2;
  33. To2[cnt2++]=u;
  34. }
  35. int _findfa(int v){
  36. if(v==fa[v]) return fa[v];
  37. return fa[v]=_findfa(fa[v]);
  38. }
  39. void _tarjan(int v)
  40. {
  41. vis[v]=true;fa[v]=v;
  42. for(int i=Laxt[v];i!=-;i=Next[i]){
  43. if(!vis[To[i]]){
  44. dis[To[i]]=dis[v]+Len[i];
  45. _tarjan(To[i]);
  46. fa[To[i]]=v;
  47. }
  48. }
  49. for(int i=Laxt2[v];i!=-;i=Next2[i]){
  50. if(vis[To2[i]]){
  51. int tmp=_findfa(To2[i]);
  52. if(dis[To2[i]]!=-)
  53. ans[i/]=dis[v]+dis[To2[i]]-*dis[tmp];
  54. else ans[i/]=-;
  55. }
  56. }
  57. }
  58. int main()
  59. {
  60. int n,m,c,i,x,y,z;
  61. while(~scanf("%d %d %d",&n,&m,&c)){
  62. _update();
  63. for(i=;i<m;i++){
  64. scanf("%d%d%d",&x,&y,&z);
  65. _add(x,y,z);
  66. _add(y,x,z);
  67. }
  68. for(i=;i<c;i++){
  69. scanf("%d%d",&x,&y);
  70. _add2(x,y);
  71. }
  72. for(i=;i<=n;i++){
  73. if(!vis[i]){
  74. memset(dis,-,sizeof(dis));
  75. dis[i]=;
  76. _tarjan(i);
  77. }
  78. }
  79. for(i=;i<c;i++)
  80. if(ans[i]==-) printf("Not connected\n");
  81. else printf("%d\n",ans[i]);
  82. }
  83. return ;
  84. }

HDU2874Connections between cities( LCA )Tarjan的更多相关文章

  1. 最近公共祖先(LCA)---tarjan算法

    LCA(最近公共祖先).....可惜我只会用tarjan去做 真心感觉tarjan算法要比倍增算法要好理解的多,可能是我脑子笨吧略略略 最近公共祖先概念:在一棵无环的树上寻找两个点在这棵树上深度最大的 ...

  2. luogu3379 【模板】最近公共祖先(LCA) Tarjan

    LCA的Tarjan算法是一个离线算法,复杂度$O(n+q)$. 我们知道Dfs搜索树时会形成一个搜索栈.搜索栈顶节点cur时,对于另外一个节点v,它们的LCA便是v到根节点的路径与搜索栈开始分叉的那 ...

  3. [HDOJ2874]Connections between cities(LCA, 离线tarjan)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 这题有不连通的情况,特别注意. 觉得是存query的姿势不对,用前向星存了一遍,还是T…… /* ...

  4. 洛谷 P3379 【模板】最近公共祖先(LCA)Tarjan离线

    题目链接:LCA tarjan离线 这道题目WA无数发,最后还是参考了大神的blog 谁会想到因为一个输入外挂WA呢 大概是我的挂是假挂吧...orz(其实加上外挂,速度提升很多) 用链式前向星保存边 ...

  5. Connections between cities(LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目: Problem Description After World War X, a lot ...

  6. HDU 2874 Connections between cities(LCA)

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

  7. 【HDU 2874】Connections between cities(LCA)

    dfs找出所有节点所在树及到树根的距离及深度及父亲. i和j在一棵树上,则最短路为dis[i]+dis[j]-dis[LCA(i,j)]*2. #include <cstring> #in ...

  8. 洛谷P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 152通过 532提交 题目提供者HansBug 标签 难度普及+/提高 提交  讨论  题解 最新讨论 为什么还是超时.... 倍增怎么70!!题解好像有 ...

  9. 图论--最近公共祖先问题(LCA)模板

    最近公共祖先问题(LCA)是求一颗树上的某两点距离他们最近的公共祖先节点,由于树的特性,树上两点之间路径是唯一的,所以对于很多处理关于树的路径问题的时候为了得知树两点的间的路径,LCA是几乎最有效的解 ...

随机推荐

  1. php中0与空 Null false的区别

    <?php $test=0; if($test==''){  echo '<br />在php中,0即为空'; //被输出 } if($test===''){  echo '< ...

  2. Mybatis使用generatedKey在插入数据时返回自增id始终为1,自增id实际返回到原对象当中的问题排查

    今天在使用数据库的时候,遇到一个场景,即在插入数据完成后需要返回此数据对应的自增主键id,但是在使用Mybatis中的generatedKey且确认各项配置均正确无误的情况下,每次插入成功后,返回的都 ...

  3. BUG克星:几款优秀的BUG跟踪管理软件

    Bug管理是指对开发,测试,设计等过程中一系列活动过程中出现的bug问题给予纪录.审查.跟踪.分配.修改.验证.关闭.整理.分析.汇总以及删除等一系列活动状态的管理.,最后出相应图表统计,email通 ...

  4. 推荐一个js脚本的字体拟合模型

    推荐一个js脚本的字体拟合模型 http://r3mi.github.io/poly2tri.js/   推荐一个js脚本的字体拟合模型 http://r3mi.github.io/poly2tri. ...

  5. 爬虫概要及web微信请求分析

    一.爬虫概要 1.网络爬虫是什么 百度百科:网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常 ...

  6. 20145311 实验一 "Java开发环境的熟悉"

    20145311 实验一 "Java开发环境的熟悉" 程序设计过程 实验内容 -实现四则运算,并进行测试 编写代码 1.四则运算就四种运算,我就做了个简单的,输入两个数,然后选择一 ...

  7. 51Nod 1419 最小公倍数挑战

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1419 题意: 思路: 要想最大,肯定去找尽量大的互质的数,如果不是互质的 ...

  8. c++ 重设容器的长度(resize)

    #include <iostream> #include <vector> using namespace std; int main () { vector<int&g ...

  9. php 二维数组

    <?php // 一个二维数组 $cars=array ( array(,), array(,), array(,) ); ?>

  10. Decorator(装饰)

    意图: 动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator 模式相比生成子类更为灵活. 适用性: 在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职责. 处理那些可以撤消 ...