HDU - 2874 Connections between cities (LCA)

Posted 西北会法语

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 2874 Connections between cities (LCA)相关的知识,希望对你有一定的参考价值。

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.InputInput 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.OutputFor 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.

        
 求连通分量,再用LCA求最短路径即可
  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<algorithm>
  5 
  6 using namespace std;
  7 
  8 const int maxn=20005;
  9 const int LOG = 20;
 10 int par[maxn][LOG],dep[maxn];
 11 int head[maxn],dis[maxn],t[maxn];
 12 int cnt=0;
 13 
 14 struct EDGE
 15 {
 16     int v,w,next;
 17 }edge[maxn*2];
 18 
 19 int find(int x)
 20 {
 21     if(t[x]!=x)
 22         t[x]=find(t[x]);
 23     return t[x];
 24 }
 25 
 26 void mix(int x,int y)
 27 {
 28     int xx=find(x);
 29     int yy=find(y);
 30     if(xx!=yy)
 31         t[xx]=yy;
 32 }
 33 
 34 void addedge(int u,int v,int w)
 35 {
 36     edge[cnt].v=v;
 37     edge[cnt].w=w;
 38     edge[cnt].next=head[u];
 39     head[u]=cnt++;
 40 }
 41 
 42 void dfs(int u,int fa,int depth){
 43     dep[u]=depth;
 44     if(depth==0)
 45     { 
 46            for(int i=0;i<LOG;i++) 
 47                par[u][i]=u;
 48     }
 49         else
 50         {
 51             par[u][0]=fa; 
 52             for(int i=1;i<LOG;i++) par[u][i]=par[par[u][i-1]][i-1];
 53         }
 54     for(int i=head[u];i!=-1;i=edge[i].next)
 55     {
 56         int q=edge[i].v;
 57         if(q==fa) continue;
 58         dis[q]=dis[u]+edge[i].w;
 59         dfs(q,u,depth+1);
 60     }
 61 }
 62 
 63 int up(int x,int step){ 
 64     for(int i=0;i<LOG;i++) 
 65         if(step&(1<<i))
 66             x=par[x][i];
 67     return x;
 68 }
 69 
 70 int lca(int u,int v)
 71 {
 72     if(dep[u]<dep[v]) 
 73         swap(u,v);   
 74     u=up(u,dep[u]-dep[v]);      
 75     if(u==v) return u;    
 76     for(int i=LOG-1;i>=0;i--)
 77     {  
 78         if(par[u][i]!=par[v][i])
 79         {
 80             u=par[u][i];
 81             v=par[v][i];
 82         }
 83     }
 84     return par[u][0];
 85 }
 86 
 87 int main()
 88 {
 89     int n,m,q;
 90     while(~scanf("%d%d%d",&n,&m,&q))
 91     {
 92         cnt=0;
 93         memset(head,-1,sizeof(head));
 94         memset(par,0,sizeof(par));
 95         memset(dis,0,sizeof(dis));
 96         for(int i=0;i<=n;i++)
 97             t[i]=i;
 98         int x,y,z;
 99         for(int i=0;i<m;i++)
100         {
101             scanf("%d%d%d",&x,&y,&z);
102             addedge(x,y,z);
103             addedge(y,x,z);
104             mix(x,y);
105         }
106         for(int i=1;i<=n;i++)
107             if(t[i]==i)
108                 dfs(i,-1,0);
109         for(int i=0;i<q;i++)
110         {
111             scanf("%d%d",&x,&y);
112             if(find(x)!=find(y))
113                 printf("Not connected\n");
114                 else
115                     printf("%d\n",dis[x]+dis[y]-2*dis[lca(x,y)]);
116         }
117     }
118     
119     return 0;
120 }

 

以上是关于HDU - 2874 Connections between cities (LCA)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2874 Connections between cities(LCA离线算法实现)

[HDU2874]Connections between cities

[HDU2874]Connections between cities

HDU2874 Connections between cities(并查集+倍增LCA算法求森林最短路)

Connections between cities(LCA)

hdu2874(tarjan)