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 ;
}

HDU2874 LCA Tarjan的更多相关文章

  1. hdu2874(lca / tarjan离线 + RMQ在线)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意: 给出 n 个顶点 m 条边的一个森林, 有 k 个形如 x y 的询问, 输出 x, ...

  2. HDU 2874 Connections between cities(LCA Tarjan)

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

  3. POJ 1986 Distance Queries(LCA Tarjan法)

    Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...

  4. LCA Tarjan方法

    LCA Tarjan方法 不得不说,高中生好厉害,OI大佬,感觉上个大学好憋屈啊! 说多了都是眼泪 链接拿去:http://www.cnblogs.com/JVxie/p/4854719.html

  5. LCA tarjan+并查集POJ1470

    LCA tarjan+并查集POJ1470 https://www.cnblogs.com/JVxie/p/4854719.html 不错的一篇博客啊,让我觉得LCA这么高大上的算法不是很难啊,嘻嘻嘻 ...

  6. hihoCoder #1067 : 最近公共祖先·二 [ 离线LCA tarjan ]

    传送门: #1067 : 最近公共祖先·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站 ...

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

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

  8. hdu2874 LCA

    题意:现在有 n 个点与 m 条边的无向无环图,但是图不一定完全连通,边有各自的边权,给出多组询问,查询两点之间的路径权值和,或者输出两点不连通. 一开始有最短路的想法,但是由于询问有 1e6 组,做 ...

  9. LA 5061 LCA tarjan 算法

    题目大意: 给定所有点的权值都为0,给定一棵树以后,每次询问都要求给定两点 x , y 和一个权值w,要求x,y路径上所有点权值加上w,最后求出每一个节点的值 这里因为查询和点都特别多,所以希望能最后 ...

随机推荐

  1. 【深入Java虚拟机】之七:Javac编译与JIT编译

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/18009455 编译过程 不论是物理机还是虚拟机,大部分的程序代码从开始编译到最终转化成物理 ...

  2. 团队作业4——第一次项目冲刺(Alpha版本) Day6

    首先和助教及老师表示抱歉,博客确实当时就写了,但是一直不算写好,因为这几天卡住了,预计实现的功能实现不了,进度跟不上,现在也在寻求解决方法. 1.站立式会议: 2. Leangoo任务分解图: 3.任 ...

  3. Android事件机制

    一句话描述: 用户和程序之间的互动机制 什么是事件? 用户和程序交互时触发的程序操作. 只要是事件,必须具备三方面: 1 事件的发生者 2 事件接受者 3 事件触发和传递 事件处理的方法 观察者模式: ...

  4. 201521123063 《Java程序设计》 第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 读操作 (1)读取控制台输入: BufferedReader br = new BufferedReader( ...

  5. 201521123121 《Java程序设计》第14周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 数据库的基本特点 1.实现数据共享 数据共享包含所有用户可同时存取数据库中的数据,也包括用户可以用各种方式通过接 ...

  6. 今天的第一个程序-南阳acm输入三个数排序

    #include<stdio.h>main(){    int a,b,c,t;    scanf("%d%d%d",&a,&b,&c);    ...

  7. Oracle总结第二篇【视图、索引、事务、用户权限、批量操作】

    前言 在Oracle总结的第一篇中,我们已经总结了一些常用的SQL相关的知识点了-那么本篇主要总结关于Oralce视图.序列.事务的一些内容- 在数据库中,我们可以把各种的SQL语句分为四大类- (1 ...

  8. JDBC第一篇--【介绍JDBC、使用JDBC连接数据库、简单的工具类】

    1.什么是JDBC JDBC全称为:Java Data Base Connectivity,它是可以执行SQL语句的Java API 2.为什么我们要用JDBC 市面上有非常多的数据库,本来我们是需要 ...

  9. GNU/Linux-MariaDB

    第一章 基础知识 基本术语 数据库(Database) 存储已经组织好的数据的 容器(通 常是 一个文件或者文件集) 人们经常使用术语"数据库"来指代他们运行的数据库软件,这是错 ...

  10. dotnet core 2.0在ubuntu下安装失败

    在ubuntu下安装.net core2.0失败了,不知道是什么原因.按照微软官方的步骤.似乎走不通.偶然翻到debian的安装方法,发现debian系统居然是直接下载包安装的.没经过apt.尝试一把 ...