HDU 2874 Connections between cities 在森林里面求最小距离
来源:互联网 发布:网络歌曲大赛 编辑:程序博客网 时间:2024/06/06 02:38
点击打开链接
题意是说给你一个森林,让你求两点之间的最近距离。
lca求最近公共祖先,如果不是在同一棵树上,则输出Not connected。
Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3528 Accepted Submission(s): 1020
Total Submission(s): 3528 Accepted Submission(s): 1020
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.
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 21 3 22 4 35 2 31 44 5
Sample Output
Not connected6HintHintHuge input, scanf recommended.
Source
2009 Multi-University Training Contest 8 - Host by BJNU
Recommend
gaojie
题意是说给你一个森林,让你求两点之间的最近距离。
lca求最近公共祖先,如果不是在同一棵树上,则输出Not connected。
#include<stdio.h>#include<string.h>#define M 10007#define N 2222212int pre[M],dis[M],vis[M],ans[N],id[M];int s1[N],s2[N],t[N],d[N],p[N];int n,m,ne,cnt;int find(int x){ while(x!=pre[x]) x=pre[x]; return x;}void add(int u,int v,int w,int h[]){ t[ne]=v,d[ne]=w,p[ne]=h[u],h[u]=ne++; t[ne]=u,d[ne]=w,p[ne]=h[v],h[v]=ne++;}void Tarjan(int u){ id[u]=cnt; pre[u]=u; vis[u]=1; for(int i=s2[u];i;i=p[i]) { int v=t[i]; if(vis[v]) { if(id[u]==id[v])//在同一棵树下 { int rt=find(v);//最近公共祖先 ans[d[i]]=dis[u]+dis[v]-2*dis[rt]; } else ans[d[i]]=-1; } } for(int i=s1[u];i;i=p[i]) { int v=t[i]; if(!vis[v]) { dis[v]=dis[u]+d[i]; Tarjan(v); pre[v]=u; } }}int main(){ //freopen("in.txt","r",stdin); int n,m,q,i,j; while(scanf("%d%d%d",&n,&m,&q)!=EOF) { for(i=1,ne=1;i<=n;i++) { s1[i]=s2[i]=vis[i]=ans[i]=id[i]=pre[i]=0; } int u,v,w; for(i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); add(u,v,w,s1); } for(i=1;i<=q;i++) { scanf("%d%d",&u,&v); add(u,v,i,s2); } for(i=1,cnt=1;i<=n;i++,cnt++) if(!vis[i]) { dis[i]=0; Tarjan(i); } for(i=1;i<=q;i++) if(ans[i]>=0) printf("%d\n",ans[i]); else printf("Not connected\n"); } return 0;}
- HDU 2874 Connections between cities 在森林里面求最小距离
- HDU 2874 Connections between cities
- hdu 2874 Connections between cities
- HDU 2874 Connections between cities
- hdu 2874 Connections between cities
- hdu 2874 Connections between cities
- HDU 2874 Connections between cities
- HDU 2874 Connections between cities
- HDU-2874-Connections between cities
- HDU 2874 Connections between cities
- HDU Connections between cities
- Connections between cities HDU
- Connections between cities HDU
- hdu 2874 Connections between cities LCA
- HDU 2874 Connections between cities [LCA]
- hdu 2874 Connections between cities (LCA)
- hdu 2874 Connections between cities(LCA)
- 【HDU】2874 Connections between cities 离线LCA
- java线程同步的各种方法
- c++模拟反射机制-方法2
- 黑马程序员_银行业务调度系统 _11
- android仅创建一个硬盘逻辑盘
- linux常用的makefile模版编写
- HDU 2874 Connections between cities 在森林里面求最小距离
- C# 提取字体点阵字模数据
- grunt-contrib-watch 结合 connect-livereload 实现修改文件后,理解刷新页面
- jersey参数拦截注入
- Spring AOP与动态代理
- JBossESB教程(四)——如何监测一个文件夹下(本地)
- 函数局部变量所需存储空间分配
- 随笔
- /etc/ld.so.conf 文件介绍