题目链接:

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. PM2.5

    http://baike.baidu.com/view/1423678.htm PM2.5是指大气中直径小于或等于2.5微米的颗粒物,也称为可入肺颗粒物.虽然PM2.5只是地球大气成分中含量很少的组分 ...

  2. ES6使用箭头函数注意点

    新事物也是有两面性的,箭头函数有他的便捷有他的优点,但是他也有缺点,他的优点是代码简洁,this提前定义,但他的缺点也是这些,比如代码太过简洁,导致不好阅读,this提前定义,导致无法使用JS进行一些 ...

  3. linux 静态库使用经验

    在编写程序的过程中,对于一些接口往往抽象成lib库的形式,甚至有些程序只有一个主程序,其他接口的调用都是库的形式存在.较多的使用库会比较利于程序的维护,因为我们的程序都可以被其他的人使用,但是往往库的 ...

  4. 启动Eclipse时,启不起来JVM terminated. Exit code=-1

    启动Eclipse时,启不起来JVM terminated. Exit code=-1 出现错误了,不知道什么原因原本好好的Eclipse,今天早上出问题了,启动不起来还抛出JVM terminate ...

  5. idea 的IDE

    idea 是与eclipse齐名的IDE(集成开发工具),以智能闻名,不过对于熟悉eclipse的的用户来说,初次接触idea有些让人搞不清方向,下面介绍一下简单的使用 方式. 1.安装 官网下载ul ...

  6. NUTCH2.3 hadoop2.7.1 hbase1.0.1.1 solr5.2.1部署(二)

     Precondition: hadoop 2.7.1 hbase 1.0.1.1 / hbase 0.98.13 192.168.1.106 ->master 192.168.1.105 ...

  7. 自我总结- CGAffineTransform

    在应用中我们经常需要做一些仿射变换 可以用于 平移.旋转.缩放变换路径: View有一个属性transform 可以指定一个 CGAffineTransform 即可完成仿射变换 1.平移变换 // ...

  8. TP框架---thinkphp模型

    1.获取系统常量信息的方法:在控制器DengLuController里面下写入下面的方法,然后调用该方法. public function test() { //echo "这是测试的&qu ...

  9. COGS1532. [IOI2001]移动电话

    1532. [IOI2001]移动电话 ★☆   输入文件:mobilephones.in   输出文件:mobilephones.out   简单对比时间限制:5 s   内存限制:256 MB [ ...

  10. HTML5(石头剪刀布游戏开发)

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...