题目链接:

Connections between cities

Time Limit: 10000/5000 MS (Java/Others)    

Memory Limit: 32768/32768 K (Java/Others)

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
 
 
题意:
 
给了一些边,可能是一棵树,可能是森林,c个询问,问i和j之间的距离;
 
思路:
 
由于是森林,所以采用lca的离线算法Tarjan;开两个并查集,一个可以先预处理是否在一棵树上,另一个用在Tarjan上;
Tarjan算法的思想是一边处理,一边回答询问,把一个节点的所有子树处理完后就可以回答完关于这个节点的和所有已经处理完点的询问了;
 
AC代码:
 
/*
2874 1731MS 29448K 2286 B G++ 2014300227
*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
typedef long long ll;
const double PI=acos(-1.0);
int n,m,c,cnt,head[N],a,b,num,pre[N],p[N],l,r,v,dis[N],ans[],vis[N],fa[N];
struct Edge
{
int to,va,next;
};
Edge edge[*N];
struct ques
{
int to,next,id;
};
ques que[+];
void add(int s,int e,int v)
{
//edge[cnt].fr = s;
edge[cnt].to = e;
edge[cnt].va=v;
edge[cnt].next = head[s];
head[s] = cnt++;//学会了这种存边的方法;
}
void q_add(int s,int e,int order)
{
//que[num].fr = s;
que[num].to = e;
que[num].next = pre[s];
que[num].id=order;
pre[s] = num++;
}
int findset(int x)
{
if(x == p[x])return x;
return p[x] = findset(p[x]);
}
int fun(int x)
{
if(x==fa[x])return x;
return fa[x]=fun(fa[x]);
}
void Tarjan(int x,int dist)
{
vis[x] = ;
dis[x]=dist;
for(int i = head[x];i!=-;i = edge[i].next)//head[x]指向以x为端点的一条边;下面的pre[x]也是相同的道理;
{
int y = edge[i].to;
if(!vis[y])
{
Tarjan(y,dist+edge[i].va);
fa[y] = x;
}
}//前边表示这个节点的所有子树已经处理完毕,下面可以回答相关的询问了;
for(int i = pre[x];i!=-;i = que[i].next)
{
int y = que[i].to;
if(findset(x) == findset(y))
{
if(vis[y])
ans[que[i].id] = dis[y]+dis[x]-*dis[fun(y)];
}
else ans[que[i].id] = -;
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&c)!=EOF)
{
for(int i = ;i <= n;i++)
{
p[i] = i;
fa[i] = i;
head[i]=pre[i]=-;
vis[i]=;
}
cnt = ;
num = ;
for(int i = ;i < m;i++)
{
scanf("%d%d%d",&l,&r,&v);
int fx=findset(l),fy=findset(r);
if(fx!=fy)p[fx]=fy;//看是否在一个树上的并查集
add(l,r,v);
add(r,l,v);
}
for(int i = ;i < c;i++)
{
scanf("%d%d",&a,&b);
q_add(a,b,i);
q_add(b,a,i);
}
for(int i=;i<=n;i++)
{
if(!vis[i])
{
Tarjan(i,);
}
}
for(int i = ;i < c;i++)
{
if(ans[i]>-)printf("%d\n",ans[i]);
else printf("Not connected\n");
}
}
return ;
}

hdu-2874 Connections between cities(lca+tarjan+并查集)的更多相关文章

  1. HDU 2874 Connections between cities(LCA Tarjan)

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

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

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

  3. HDU 2874 Connections between cities (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意是给你n个点,m条边(无向),q个询问.接下来m行,每行两个点一个边权,而且这个图不能有环路 ...

  4. LCA tarjan+并查集POJ1470

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

  5. HDU 2874 Connections between cities(LCA)

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

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

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

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

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

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

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

  9. HDU——2874 Connections between cities

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

随机推荐

  1. 一种把dll放在不同目录的巧妙方法

    想必C#的开发者都遇到过这个问题,引用的dll都放在根目录下,随着项目的日益增大,根目录下充满了各种各样的dll,非常的不美观. 如果能够把dll按照想要的目录来存放,那么系统就美观多了,以下是我常用 ...

  2. mysql经常使用查询:group by,左连接,子查询,having where

    前几天去了两个比較牛的互联网公司面试.在sql这块都遇到问题了,哎.可惜呀,先把简单的梳理一下 成绩表 score 1.group by 使用 按某一个维度进行分组 比如: 求每一个同学的总分 SEL ...

  3. shell脚本学习笔记 (流编辑器sed)

    sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用很普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑命令转换为另一种格式输出. sed不 ...

  4. IOS研究之网络编程(二)-Cocoa Streams使用具体解释

     本文以及相关的系列文章是我总结的iOS网络开发方面的知识点,本文是第二篇,主要分析了Cocoa Streams中的几个重要类 Cocoa Streams实际上是Objective-C对CFNet ...

  5. js错误: Unexpected number in JSON at position 2792 value里面有双引号怎么解决

    源头  出现这个报错提示,大家从错误就可以看的出来,这就是json的错误,一般来说都是json格式出现了错误,本人遇到比较多的情况就是json字符串里面出现了一些会影响json格式的符号,这次出现这个 ...

  6. caffe--anaconda2--makefile.config--compile --ubuntu16.04

    sea@sea-X550JK:/media/sea/wsWin10/wsUbuntu16.04/DlFrames/caffe$ cat Makefile.config: ## Refer to htt ...

  7. ZIP解压缩文件的工具类【支持多级文件夹|全】

    ZIP解压缩文件的工具类[支持多级文件夹|全] 作者:Vashon 网上有非常多的加压缩演示样例代码.可是都仅仅是支持一级文件夹的操作.假设存在多级文件夹的话就不行了. 本解压缩工具类经过多次检查及重 ...

  8. 有状态的EJB对象和无状态的EJB对象

    一,定义有状态Bean和无状态Bean 有状态Bean: @Stateful @Remote public class StatefulEjbBean implements StatefulEjb{ ...

  9. Windows系统下正确安装MongoDB

    1.下载.安装 官网下载: http://www.mongodb.org/downloads 下载好之后,接下来进行安装了: 2.创建数据文件夹 MongoDB将数据文件夹存储在 db 文件夹下. 可 ...

  10. python 基础 5.3 类的重写

    一. 类的重写 只需要重新定义类的属性(变量),就是累的重写了 示例:重新定义类grandson的 name属性   #/usr/bin/python #coding=utf-8 #@Time :20 ...