博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 2874 Connections between cities (并查集+LCA)
阅读量:6361 次
发布时间:2019-06-23

本文共 4055 字,大约阅读时间需要 13 分钟。

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:            
 

 题意:

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

 

并查集+LCA:

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

在线算法:

1 //2328MS    3728K    2757 B    G++  2 #include
3 #include
4 #include
5 #define N 20005 6 struct node{ 7 int u,v,d; 8 int next; 9 }edge[2*N]; 10 int num,n,head[N]; 11 int root[2*N],rank[N]; 12 int dep[2*N],dis[N]; 13 int dp[2*N][30],vis[N]; 14 int set[N]; 15 void addedge(int u,int v,int d) 16 { 17 edge[num].u=u; 18 edge[num].v=v; 19 edge[num].d=d; 20 edge[num].next=head[u]; 21 head[u]=num++; 22 } 23 int Min(int a,int b) 24 { 25 return dep[a]
y) set[x]=y; 38 else set[y]=x; 39 } 40 void dfs(int u,int deep) 41 { 42 vis[u]=1; 43 root[++num]=u; 44 rank[u]=num; 45 dep[num]=deep; 46 for(int i=head[u];i!=-1;i=edge[i].next){ 47 int v=edge[i].v,d=edge[i].d; 48 if(vis[v]) continue; 49 dis[v]=dis[u]+d; 50 dfs(v,deep+1); 51 root[++num]=u; 52 dep[num]=deep; 53 } 54 } 55 void init() 56 { 57 int nn=2*n-1; 58 int m=(int)(log(nn*1.0)/log(2.0)); 59 for(int i=1;i<=nn;i++) 60 dp[i][0]=i; 61 for(int j=1;j<=m;j++) 62 for(int i=1;i+(1<
<=nn;i++) 63 dp[i][j]=Min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); 64 } 65 int RMQ(int l,int r) 66 { 67 int m=(int)(log((r-l+1)*1.0)/log(2.0)); 68 return Min(dp[l][m],dp[r-(1<
b) return root[RMQ(b,a)]; 75 else return root[RMQ(a,b)]; 76 } 77 int main(void) 78 { 79 int m,c,u,v,d; 80 int in[N]; 81 while(scanf("%d%d%d",&n,&m,&c)!=EOF) 82 { 83 memset(edge,0,sizeof(edge)); 84 memset(vis,0,sizeof(vis)); 85 memset(head,-1,sizeof(head)); 86 memset(in,0,sizeof(in)); 87 for(int i=0;i<=n;i++) set[i]=i; 88 num=0; 89 for(int i=0;i

 

 Tarjan离线做法:

1 //2359MS     30504K    1830B     G++     2 #include
3 #include
4 #include
5 #define N 10005 6 using namespace std; 7 struct node{ 8 int v,d; 9 node(int a,int b){10 v=a;d=b;11 } 12 };13 vector
child[N],V[N];14 int fa[N],vis[N],dis[N],mark[N];15 int res[100*N];16 int n;17 int find(int x)18 {19 if(x!=fa[x]) fa[x]=find(fa[x]);20 return fa[x];21 }22 void Tarjan(int u)23 {24 fa[u]=u;25 vis[u]=1;26 for(int i=0;i

 

 

转载于:https://www.cnblogs.com/GO-NO-1/p/3674610.html

你可能感兴趣的文章
wp8.1 Study14 FilePicker简单介绍
查看>>
Eclipse快捷键 之 代码追踪
查看>>
bzoj 1191: [HNOI2006]超级英雄Hero
查看>>
PowerBuilder中新建PBL
查看>>
Java基础14-缓冲区字节流;File类
查看>>
随笔分类 - linux系统编程
查看>>
Java字符集编码
查看>>
黄金点游戏结对
查看>>
基于 Apache 在本地配置多个虚拟主机
查看>>
Angular.js+Bootstrap实现表格分页
查看>>
关于报错:Warning: Cannot modify header information - headers already sent by (output started at
查看>>
软件工程(2018)团体第五次作业
查看>>
process xlsx with pandas
查看>>
putty 自动密码登陆【window】
查看>>
getparent方法的一些陷阱、m_pParentWnd
查看>>
call_grant_dml.sql
查看>>
20155301信息安全系统设计基础第三次实验
查看>>
转 查看磁盘IO负载 - 看哪些进程在读写磁盘 以及oracle 异步I/O 和同步I/O
查看>>
数据集的使用
查看>>
图片转换成流
查看>>