hdu 2874 Connections between cities (并查集+LCA)

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解决距离问题。

在线算法:

  1 //2328MS    3728K    2757 B    G++
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<math.h>
  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]<dep[b]?a:b;
 26 }
 27 int find(int x)
 28 {
 29     if(set[x]!=x) 
 30         set[x]=find(set[x]);
 31     return set[x];
 32 }
 33 void merge(int a,int b)
 34 {
 35     int x=find(a);
 36     int y=find(b);
 37     if(x>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<<j)-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<<m)+1][m]);
 69 }
 70 int LCA(int u,int v)
 71 {
 72     int a=rank[u];
 73     int b=rank[v];
 74     if(a>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<m;i++){
 90             scanf("%d%d%d",&u,&v,&d);
 91             addedge(u,v,d);
 92             addedge(v,u,d);
 93             in[v]++;
 94             merge(u,v);
 95         }
 96         for(int i=1;i<=n;i++) //连接森林的所有树 
 97             if(in[i]==0){
 98                 addedge(0,i,0);
 99                 addedge(i,0,0);
100             }
101         num=0;
102         dis[0]=0;
103         dfs(0,1);
104         init();
105         //for(int i=1;i<=2*n-1;i++) printf(i==2*n-1?"%d\n":"%d ",root[i]); 
106         //for(int i=1;i<=2*n-1;i++) printf(i==2*n-1?"%d\n":"%d ",dep[i]);
107         //for(int i=1;i<=2*n-1;i++) printf(i==2*n-1?"%d\n":"%d ",dis[i]);
108         //for(int i=1;i<=2*n-1;i++) printf(i==2*n-1?"%d\n":"%d ",rank[i]);
109         while(c--){
110             scanf("%d%d",&u,&v);
111             //printf("*%d %d\n",find(u),find(v));
112             if(find(u)!=find(v)) puts("Not connected");
113             else printf("%d\n",dis[u]+dis[v]-2*dis[LCA(u,v)]);
114         }
115     }
116     return 0;
117 }

 

 Tarjan离线做法:

 1 //2359MS     30504K    1830B     G++    
 2 #include<iostream>
 3 #include<queue>
 4 #include<vector>
 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<node>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<V[u].size();i++){
27         int v=V[u][i].v,d=V[u][i].d;
28         if(vis[v] && res[d]==-1 && !mark[find(v)])
29             res[d]=dis[u]+dis[v]-2*dis[find(v)];
30     }
31     for(int i=0;i<child[u].size();i++){
32         int v=child[u][i].v,d=child[u][i].d;
33         if(vis[v]) continue;
34         dis[v]=dis[u]+d;
35         Tarjan(v);
36         fa[v]=u;
37     }
38 }
39 int main(void)
40 {
41     int m,c;
42     int u,v,d;
43     while(scanf("%d%d%d",&n,&m,&c)!=EOF)
44     {
45         for(int i=0;i<=n;i++){
46             child[i].clear();
47             V[i].clear();
48             fa[i]=i;
49             vis[i]=mark[i]=0;
50         }
51         memset(res,-1,sizeof(res));
52         for(int i=1;i<=m;i++){
53             scanf("%d%d%d",&u,&v,&d);
54             child[u].push_back(node(v,d));
55             child[v].push_back(node(u,d));
56         }
57         for(int i=1;i<=c;i++){
58             scanf("%d%d",&u,&v);
59             V[u].push_back(node(v,i));
60             V[v].push_back(node(u,i));
61         }
62         for(int i=1;i<=n;i++){
63             if(!vis[i]){
64                 dis[i]=0;
65                 Tarjan(i);
66                 mark[i]=1;
67             }
68         }
69         //for(int i=1;i<=n;i++) printf(i==n?"%d\n":"%d ",dis[i]);
70         //for(int i=1;i<=n;i++) printf(i==n?"%d\n":"%d ",fa[i]);
71         for(int i=1;i<=c;i++){
72             if(res[i]==-1) puts("Not connected");
73             else printf("%d\n",res[i]);
74         }
75     }
76 }

 

 

posted @ 2014-04-19 09:37  heaventouch  阅读(1174)  评论(0编辑  收藏  举报