题目链接:

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. lambda表达式转换sql

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...

  2. oracle 表压缩技术

    压缩表是我们维护管理中常常会用到的.以下我们看都oracle给我们提供了哪些压缩方式. 文章摘自"Oracle® Database Administrator's Guide11g Rele ...

  3. wifi认证Portal开发系列(三):portal协议

    中国移动WLAN业务PORTAL协议规范介绍 一.用户上线认证流程 上线流程完成用户账号的认证,并把认证结果通知Portal Server,Portal server将会通知WLAN用户并且显示相应的 ...

  4. jdbc 简单连接

    package itcast; import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;i ...

  5. HDFS源码分析之LightWeightGSet

    LightWeightGSet是名字节点NameNode在内存中存储全部数据块信息的类BlocksMap需要的一个重要数据结构,它是一个占用较低内存的集合的实现,它使用一个数组array存储元素,使用 ...

  6. 百度富文本编辑器Ueditor上传图片时标签中添加宽高

    ueditor.all.js中:直接搜索callback() function callback(){ try{ var link, json, loader, body = (iframe.cont ...

  7. StringBuilder的append、StringBuffer的append和String str = "a"+"b"的区别?

    大家都知道String+String会开销额外的系统资源,粗略的原因是String是不可变类,每一步操作都会返回新的String变量,占用空间及时间. 其实我的理解不是这样的,我们来看看String+ ...

  8. CentOS Python 安装MySQL-python

    一.安装mysql yum list | grep mysql >>yum install -y mysql-server mysql mysql-devel CentOS 7的yum源中 ...

  9. python 基础 1.5 python数据类型(三)--元组常用方法示例

    #/usr/bin/python#coding=utf-8#@Time :2017/10/13 15:02#@Auther :liuzhenchuan#@File :元组.py #tuple() 字符 ...

  10. RSA加密、解密、公钥私钥生成

    有时项目中需要用到一些加密和解密工具,这里之前整理了一个demo,记录一下,方便查询 package com.test; import java.security.KeyFactory; import ...