题目链接:

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. (十)jQuery对表单、表格的操作

    一.表单应用 1.HTML中的表单大致由三部分组成 (1).表单标签:包含处理表单数据所用的服务端程序URL,以及数据提交到服务器的方法. (2).表单域:包含文本框.密码框.隐藏域.多行文本框.复选 ...

  2. CentOS中文乱码的问题

    修改CentOS 6.4 root用户的系统默认语言设置 最近用Virtual Box 虚拟了一个CentOS系统,版本6.4,安装时使用简体中文.发现用普通用户登录的时候 设置语言环境为Englis ...

  3. 解决phpmyadmin导入大数据库出现一系列问题

    在用phpmyadmin导入mysql数据库文件时,往往超过2M就会提示文件大,导入不成功.这时我们打开phpmyadmin-->libraries-->config.default.ph ...

  4. opencv3.3.1 opencv_contribut 3.3.1 git 20180117最新版的在ubuntu1604上的编译

    过程: 1.  git  clone  ...   contribut 2. git  clone  ...  opencv 3.  git  checkout  -b     v3.3.1 4 gi ...

  5. 03 redis之string类型命令解析

    Redis字符串类型的操作 set key value [ex 秒数] / [px 毫秒数] [nx] /[xx] 如: set a 1 ex 10 , 10秒有效 Set a 1 px 9000 , ...

  6. programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation

    编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...

  7. ios-逆向 手把手安装最新版Theos

      Theos.最初由DHowett进行开发,由于DHwoett去了微软,不再有时间维护了,所以Adam Demasi(kirb)接手了他的工作,并且添加了很多全新的功能.所以,之前书上<iOS ...

  8. Yii2 跨库orm实现

    近期在对公司的Yii2项目进行子系统拆分,过度阶段难免会有一些跨库操作,原生语句还好,加下库名前缀就可以了,可是到了orm问题就来了,特别是用到model做查询的时候,现在来记录一下跳过的坑, 像下面 ...

  9. 【BZOJ4435】[Cerc2015]Juice Junctions Tarjan+hash

    [BZOJ4435][Cerc2015]Juice Junctions Description 你被雇佣升级一个旧果汁加工厂的橙汁运输系统.系统有管道和节点构成.每条管道都是双向的,且每条管道的流量都 ...

  10. Swift 学习笔记(面向协议编程)

    在Swift中协议不仅可以定义方法和属性,而且协议是可以扩展的,最关键的是,在协议的扩展中可以添加一些方法的默认实现,就是在协议的方法中可以实现一些逻辑,由于这个特性,Swift是可以面向协议进行编程 ...