Connections between cities

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4057    Accepted Submission(s): 1178

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
Hint

Hint

Huge input, scanf recommended.

 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2873 2876 2872 2875 2877 
 

题意:

给出n个点m条边的的森林,求两个点间的最短距离。

并查集+LCA:

这题想了挺久的,觉得是比较经典的题目。首先要知道这是一个森林,和一般的LCA不同,要变成LCA来写其实也不难(没想方法,最后看别人思路才恍然大悟),就是在所有树的根节点连上一个虚拟根节点0,将森林变成树,然后之前的节点用并查集处理,用于判断是否处于同棵树,处于同一个树再用LCA解决距离问题。

在线算法:

 //2328MS    3728K    2757 B    G++
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 20005
struct node{
int u,v,d;
int next;
}edge[*N];
int num,n,head[N];
int root[*N],rank[N];
int dep[*N],dis[N];
int dp[*N][],vis[N];
int set[N];
void addedge(int u,int v,int d)
{
edge[num].u=u;
edge[num].v=v;
edge[num].d=d;
edge[num].next=head[u];
head[u]=num++;
}
int Min(int a,int b)
{
return dep[a]<dep[b]?a:b;
}
int find(int x)
{
if(set[x]!=x)
set[x]=find(set[x]);
return set[x];
}
void merge(int a,int b)
{
int x=find(a);
int y=find(b);
if(x>y) set[x]=y;
else set[y]=x;
}
void dfs(int u,int deep)
{
vis[u]=;
root[++num]=u;
rank[u]=num;
dep[num]=deep;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v,d=edge[i].d;
if(vis[v]) continue;
dis[v]=dis[u]+d;
dfs(v,deep+);
root[++num]=u;
dep[num]=deep;
}
}
void init()
{
int nn=*n-;
int m=(int)(log(nn*1.0)/log(2.0));
for(int i=;i<=nn;i++)
dp[i][]=i;
for(int j=;j<=m;j++)
for(int i=;i+(<<j)-<=nn;i++)
dp[i][j]=Min(dp[i][j-],dp[i+(<<(j-))][j-]);
}
int RMQ(int l,int r)
{
int m=(int)(log((r-l+)*1.0)/log(2.0));
return Min(dp[l][m],dp[r-(<<m)+][m]);
}
int LCA(int u,int v)
{
int a=rank[u];
int b=rank[v];
if(a>b) return root[RMQ(b,a)];
else return root[RMQ(a,b)];
}
int main(void)
{
int m,c,u,v,d;
int in[N];
while(scanf("%d%d%d",&n,&m,&c)!=EOF)
{
memset(edge,,sizeof(edge));
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
memset(in,,sizeof(in));
for(int i=;i<=n;i++) set[i]=i;
num=;
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&d);
addedge(u,v,d);
addedge(v,u,d);
in[v]++;
merge(u,v);
}
for(int i=;i<=n;i++) //连接森林的所有树
if(in[i]==){
addedge(,i,);
addedge(i,,);
}
num=;
dis[]=;
dfs(,);
init();
//for(int i=1;i<=2*n-1;i++) printf(i==2*n-1?"%d\n":"%d ",root[i]);
//for(int i=1;i<=2*n-1;i++) printf(i==2*n-1?"%d\n":"%d ",dep[i]);
//for(int i=1;i<=2*n-1;i++) printf(i==2*n-1?"%d\n":"%d ",dis[i]);
//for(int i=1;i<=2*n-1;i++) printf(i==2*n-1?"%d\n":"%d ",rank[i]);
while(c--){
scanf("%d%d",&u,&v);
//printf("*%d %d\n",find(u),find(v));
if(find(u)!=find(v)) puts("Not connected");
else printf("%d\n",dis[u]+dis[v]-*dis[LCA(u,v)]);
}
}
return ;
}

Tarjan离线做法:

 //2359MS     30504K    1830B     G++
#include<iostream>
#include<queue>
#include<vector>
#define N 10005
using namespace std;
struct node{
int v,d;
node(int a,int b){
v=a;d=b;
}
};
vector<node>child[N],V[N];
int fa[N],vis[N],dis[N],mark[N];
int res[*N];
int n;
int find(int x)
{
if(x!=fa[x]) fa[x]=find(fa[x]);
return fa[x];
}
void Tarjan(int u)
{
fa[u]=u;
vis[u]=;
for(int i=;i<V[u].size();i++){
int v=V[u][i].v,d=V[u][i].d;
if(vis[v] && res[d]==- && !mark[find(v)])
res[d]=dis[u]+dis[v]-*dis[find(v)];
}
for(int i=;i<child[u].size();i++){
int v=child[u][i].v,d=child[u][i].d;
if(vis[v]) continue;
dis[v]=dis[u]+d;
Tarjan(v);
fa[v]=u;
}
}
int main(void)
{
int m,c;
int u,v,d;
while(scanf("%d%d%d",&n,&m,&c)!=EOF)
{
for(int i=;i<=n;i++){
child[i].clear();
V[i].clear();
fa[i]=i;
vis[i]=mark[i]=;
}
memset(res,-,sizeof(res));
for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&d);
child[u].push_back(node(v,d));
child[v].push_back(node(u,d));
}
for(int i=;i<=c;i++){
scanf("%d%d",&u,&v);
V[u].push_back(node(v,i));
V[v].push_back(node(u,i));
}
for(int i=;i<=n;i++){
if(!vis[i]){
dis[i]=;
Tarjan(i);
mark[i]=;
}
}
//for(int i=1;i<=n;i++) printf(i==n?"%d\n":"%d ",dis[i]);
//for(int i=1;i<=n;i++) printf(i==n?"%d\n":"%d ",fa[i]);
for(int i=;i<=c;i++){
if(res[i]==-) puts("Not connected");
else printf("%d\n",res[i]);
}
}
}

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

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

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

  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判是否联通

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

  4. HDU——2874 Connections between cities

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

  5. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  6. HDU 2874 Connections between cities(LCA Tarjan)

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

  7. HDU 2874 Connections between cities(LCA)

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

  8. HDU 2874 Connections between cities(LCA+并查集)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2874 [题目大意] 有n个村庄,m条路,不存在环,有q个询问,问两个村庄是否可达, 如果可达则输出 ...

  9. HDU 2874 Connections between cities (LCA)

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

随机推荐

  1. 台式机上如何配置并使用苹果iPhone的耳机麦克风 并且麦克风开启降噪功能

    这个资料和技巧在网络上面很少有人分享,但是可能会有不少人需要这个东西.这里分享下经验.这也是一个困扰我很久的一个问题.因为买来了这个转接头,发现,录音的时候iPhone的耳机麦克风有很大的噪音无法消除 ...

  2. 使用Python第三方库生成二维码

    本文主要介绍两个可用于生成二维码的Python第三方库:MyQR和qrcode. MyQR的使用: 安装: pip install MyQR 导入: from MyQR import myqr imp ...

  3. Vue插槽 slot

    1. 什么是插槽 插槽slot 是往父组件中插入额外内容,实现组件的复用,一个插槽插入到一个对应的标签中 2. 实例: 一个组件中不允许有两个匿名插槽 </head> <body&g ...

  4. 通过swagger下载的文件乱码解决方法,求解

    这里的数据显示 点击Download Templates下载之后是显示一个response流都不是一个xlsx文件 这个是由什么原因造成的,求解?

  5. [转]Centos7 内核从3.10升级到4.12过程

    [原文地址] http://blog.csdn.net/youshijifen/article/details/73472434 [摘要] 近期,国家互联网应急中心漏洞(CNCERT)公告中提到Lin ...

  6. STM32CubeMx配置USART注意的一个问题

    HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, Number);意思是接收到Number个字节后,触发HAL_UART_RxCpltCal ...

  7. Java学习笔记一:三步搭建Java开发环境

    Java开发环境搭建 一:安装JDK: 1.下载地址:http://www.oracle.com/technetwork/java/javase/downloads 非常显眼的下载界面 2.点击下载后 ...

  8. idea离线安装lombock插件

    技术交流群:233513714 安装过程 1.首先找到插件包 插件包可以在两个地方下载,分别是IDEA的官方插件仓库和GitHub里lombok-intellij-plugin仓库中的release包 ...

  9. Vue-router使用

    Vue路由:--------------------------------------------------------1 .Vue-rouer入门2 .子路由3 .路由传参4 .多路由区域操作5 ...

  10. ubuntu 关闭触控板

    第一种: 1 sudo rmmod psmouse    这个是禁用的 2 sudo modprobe psmouse 这个是启用的 这个方法很便捷,但是会将触点和触板都禁用了,一般还是希望保持触点是 ...