HDOJ 题目2874 Connections between cities(LCA转RMQ+并查集)

本文探讨了在战后城市重建中如何解决材料运输的问题,特别是在道路被破坏的情况下寻找两个城市间最短路径的方法。通过一种特殊的路径查找算法,文章详细介绍了如何在没有直接连接或者存在部分损坏道路的城市间找到最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
 

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 
 
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]);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值