HDOJ 题目2874 Connections between cities(LCA转RMQ+并查集)
来源:互联网 发布:淘宝大学入口 编辑:程序博客网 时间:2024/06/10 15:52
Connections between cities
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6998 Accepted Submission(s): 1812
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 | We have carefully selected several similar problems for you: 2873 2876 2872 2875 2877
ac代码
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; #define N 10010 int vis[N]; int first[N*2],node[N*2],deep[N*2],minv[N<<1][25],hah[N],pre[N],dis[N]; int n,q,cnt,m; int head[N]; struct s { int u,v,w,next; }edge[N<<1]; void add(int u,int v,int w) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; } int tot; void dfs(int u,int dep) { tot++; // fa[u]=pre; node[tot]=u; deep[tot]=dep; vis[u]=1; first[u]=tot; int i; for(i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(!vis[v]) { dis[v]=dis[u]+edge[i].w; dfs(v,dep+1); tot++; node[tot]=u; deep[tot]=dep; } } } void init_RMQ(int n) { int i,j,k; for(i=1;i<=n;i++) { minv[i][0]=i; } int kk = (int) (log((double) n) / log(2.0)); for(j=1;j<=kk;j++) { for(k=1;k+(1<<j)-1<=n;k++) { if(deep[minv[k][j-1]]>deep[minv[k+(1<<(j-1))][j-1]]) minv[k][j]=minv[k+(1<<(j-1))][j-1]; else minv[k][j]=minv[k][j-1]; } } } int q_min(int l,int r) { int k=(int)(log((double)(r-l+1))/(log(2.0))); if(deep[minv[l][k]]>deep[minv[r-(1<<k)+1][k]]) return minv[r-(1<<k)+1][k]; else return minv[l][k]; } int lca(int a,int b) { int x=first[a]; int y=first[b]; int k; if(x<y) { k=q_min(x,y); } else k=q_min(y,x); return node[k]; } void init(){int i;for(i=0;i<=n;i++){pre[i]=i;node[i]=0;}memset(first,0,sizeof(first));}int find(int x){if(x==pre[x])return x;return pre[x]=find(pre[x]);}void merge(int x,int y){int fx=find(x);int fy=find(y);if(fx!=fy)pre[fx]=fy;}int main(){//int n,m,q;while(scanf("%d%d%d",&n,&m,&q)!=EOF){memset(head,-1,sizeof(head));cnt=0;int i,j;init();for(i=1;i<=m;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);add(a,b,c);add(b,a,c);merge(a,b);}memset(hah,0,sizeof(hah));for(i=1;i<=n;i++){if(pre[i]==i){add(0,i,0);add(i,0,0);}}tot=0;memset(vis,0,sizeof(vis));dis[0]=0;dfs(0,0);init_RMQ(tot-1);while(q--){int a,b;scanf("%d%d",&a,&b);if(find(a)!=find(b)){printf("Not connected\n");continue;}int c=lca(a,b);printf("%d\n",dis[a]+dis[b]-2*dis[c]);}}}
0 0
- HDOJ 题目2874 Connections between cities(LCA转RMQ+并查集)
- hdoj 2874 Connections between cities 【并查集合并森林成一棵树 + LCA转RMQ】
- HDU2874——Connections between cities 详解 (LCA,RMQ,数据结构,dfs序,并查集)
- hdu 2874 Connections between cities LCA || dfs+并查集
- |hdu 2874|LCA|并查集|Connections between cities
- hdu2874 Connections between cities(LCA离线+并查集)
- HDU 2874 —— Connections between cities(并查集+LCA)
- 文章标题 HDU 2874 : Connections between cities(LCA+并查集)
- HDU 2874 Connections between cities(LCA + RMQ)
- 【LCA转RMQ ST】 HDU 2874 Connections between cities
- HDU2874 Connections between cities LCA+DFS或并查集
- hdu 2874 Connections between cities (并查集+LCA)hash优化
- Connections between cities (hdu 2874 LCA)
- hdu 2874 Connections between cities(LCA)
- HDU 2874Connections between cities(LCA)
- hdoj 2874 Connections between cities 【Tarjan离线LCA】
- HDU 2874 Connections between cities (LCA离线&&在线RMQ,4级)
- HDOJ 2874 Connections between cities
- SSM+Nodejs
- 计算窗口边框厚度、标题栏高度最正确的方法
- 黑马程序员——多态与继承
- es 一 ---- elasticsearch1.5安装使用+IK中文分词
- Android dispatchTouchEvent touch事件的分发
- HDOJ 题目2874 Connections between cities(LCA转RMQ+并查集)
- 充电桩通过WiFi付费和管理方案
- 2111:Saving HDU
- Phpstorm FTP sftp
- tomcat catalina.bat解读
- 华为oj-字符逆序
- 为你的Android App实现自签名的 SSL 证书
- 快速排序-冒泡法改进
- 非确定型有限自动机变换为确定型有限自动机