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.

#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#include <cstdio>
#include <memory.h>
#include<vector>
using namespace std;
int Laxt[],Next[],To[],Len[];
int Laxt2[],Next2[],To2[],ans[];
bool vis[];
int cnt,cnt2;
int dis[],fa[];
void _update()
{
memset(Laxt,-,sizeof(Laxt));
memset(Laxt2,-,sizeof(Laxt2));
memset(vis,false,sizeof(vis));
cnt=cnt2=;
}
void _add(int u,int v,int d){
Next[cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
Len[cnt++]=d;
}
void _add2(int u,int v){
Next2[cnt2]=Laxt2[u];
Laxt2[u]=cnt2;
To2[cnt2++]=v;
Next2[cnt2]=Laxt2[v];
Laxt2[v]=cnt2;
To2[cnt2++]=u;
}
int _findfa(int v){
if(v==fa[v]) return fa[v];
return fa[v]=_findfa(fa[v]);
}
void _tarjan(int v)
{
vis[v]=true;fa[v]=v;
for(int i=Laxt[v];i!=-;i=Next[i]){
if(!vis[To[i]]){
dis[To[i]]=dis[v]+Len[i];
_tarjan(To[i]);
fa[To[i]]=v;
}
}
for(int i=Laxt2[v];i!=-;i=Next2[i]){
if(vis[To2[i]]){
int tmp=_findfa(To2[i]);
if(dis[To2[i]]!=-)
ans[i/]=dis[v]+dis[To2[i]]-*dis[tmp];
else ans[i/]=-;
}
}
}
int main()
{
int n,m,c,i,x,y,z;
while(~scanf("%d %d %d",&n,&m,&c)){
_update();
for(i=;i<m;i++){
scanf("%d%d%d",&x,&y,&z);
_add(x,y,z);
_add(y,x,z);
}
for(i=;i<c;i++){
scanf("%d%d",&x,&y);
_add2(x,y);
}
for(i=;i<=n;i++){
if(!vis[i]){
memset(dis,-,sizeof(dis));
dis[i]=;
_tarjan(i);
}
}
for(i=;i<c;i++)
if(ans[i]==-) printf("Not connected\n");
else printf("%d\n",ans[i]);
}
return ;
}

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. 新版samba安装过程

    yum install samba samba-client samba-swat cp -p /etc/samba/smb.conf    /etc/samba/smb.conf.orig /bin ...

  2. 497. Random Point in Non-overlapping Rectangles

    1. 问题 给定一系列不重叠的矩形,在这些矩形中随机采样一个整数点. 2. 思路 (1)一个矩形的可采样点个数就相当于它的面积,可以先依次对每个矩形的面积累加存起来(相当于概率分布中的分布累积函数CD ...

  3. docker 删除所有的 docker ps -a 记录

    docker rm `docker ps -a -q` 删除的是镜像运行的实例,要删除镜像用docker rmi + 镜像名或者那串字符(image  id)

  4. 20145122《Java程序设计》第6周学习总结

    教材学习内容总结 1.Java是以串流(Stream)的方式来处理输入与输出. 2.串流是一种抽象观念,从键盘输入资料,将处理结果输入档案,以及读取档案的内容等动作皆视为串流的处理. 3.在JAVA中 ...

  5. 20145326《Java程序设计》第一周学习总结

    20145326<Java程序设计>第一周学习总结 教材学习内容总结 转眼间新的一学期终于开始了!为什么我这么期待呢?因为这学期可以上娄嘉鹏老师的java程序设计课,我不是什么电脑天才,之 ...

  6. Codeforces Round #319 (Div. 2) B. Modulo Sum 抽屉原理+01背包

    B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. iframe与父页面传值

    最近做的项目中用到了不少iframe,而且需要实现: 从父页面获取iframe中某个元素的值或则从iframe页面获取其父页面上某个元素的值. 在网上查询了相关东西,后总结如下: (1)父页面获取if ...

  8. VuePress从零开始搭建自己的博客

    VuePress是什么? VuePress是以Vue驱动的静态网站生成器,是一个由Vue.Vue Router和webpack驱动的单页应用.在VuePress中,你可以使用Markdown编写文档, ...

  9. 使用SpringMVC自带的@Scheduled完成定时任务

    首先在xml配置文件头中添加以下几行: xmlns:task="http://www.springframework.org/schema/task"   http://www.s ...

  10. 过滤器系列(三)—— RSQF

    这个过滤器本身是一篇论文中提出的过滤器的简化版本,去掉了计数功能,我觉得简化版本应用的可能也很广,专门写一篇简化版本的RSQF.RSQF全称是rank-and-select based filter, ...