题目链接:

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. .net 定时执行 windows 服务

    1.新建项目 --> Windows 服务 2.Service1.cs代码 using System; using System.Collections.Generic; using Syste ...

  2. PX4学习之-uORB简单体验

    一.前言 最近项目使用到 CPU2 与 CPU0 之间的通信, 使用定时器传递消息到 CPU0 后, CPU0 需要将消息分发到不同的应用程序里面. PX4 里面使用的是 uORB 多线程/进程通信机 ...

  3. ROR部署到Heroku出现Application Error和code=H10 desc=&quot;App crashed“问题

    1.问题发现之前的准备 在读<Learn Python In Hard Way>的时候,发现作者谈到一个非常有趣的事情,在做一些有趣的事情之前做的无聊的事情叫做yak shaving,牦牛 ...

  4. android开发系列之使用xml自定义控件

    在android开发的过程中,有的时候面对多个Activity里面一些相同的布局,我们需要写多次相同的代码,同时这种方法给我们的项目维护也带来了很大不便.那么有没有一种可行的办法能够将Activity ...

  5. spark之pycharm开发设置

    方法一: __author__ = 'similarface' import os import sys os.environ['SPARK_HOME']="/Users/similarfa ...

  6. 对你的 REST API 进行保护的正确办法

    设计好一个美丽的 REST + JSON API 之后,怎样对你的 API 进行保护?在 Stormpath,我们花了 18 个月来寻找最佳实践.将其一一实践于 Stormpath API 中并分析其 ...

  7. 点击textbox弹出对话框,返回弹出对话框的值

    主要是在父页面使用 function PopupWindow() {            window.open(url, "", "status=no,resizab ...

  8. EasyNVR摄像机H5流媒体服务器在windows上批处理脚本自动以管理员权限运行

    很多时候, 我们需要以管理员权限来运行批处理脚本, 比如操作 windows 服务. EasyNVR 中提供安装服务的批处理脚本, 运行这个bat文件, 自动将 EasyNVR 以 windows 服 ...

  9. OpenCV Machine Learning 之 正态贝叶斯分类器 (Normal Bayes Classifier)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhjm07054115/article/details/27631913

  10. MySQL修改配置 区分大小写

    在使用mysql的时候,数据库名,表名,字段名等有大小写的区分,这个可以通过配置文件设置.如果设置了严格区分大小写,在访问表的时候没有注意到表名的大小写,将会报出表不存在的错误.下面是修改配置文件: ...