Design the city ZOJ - 3195 LCA倍增

Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.

In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.

Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.

 

Input

 

The input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there's a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.

Process to the end of file.

 

Output

 

Q lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.

Output a blank line between each test cases.

 

Sample Input

 

4
0 1 1
0 2 1
0 3 1
2
1 2 3
0 1 2
5
0 1 1
0 2 1
1 3 1
1 4 1
2
0 1 2
1 0 3

 

Sample Output

 

3
2

2
2

题意:给你一个无向图,求图中三点连起来最短距离。

思路:两两之间求一次LCA并计算出距离,然后相加除以2

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=100009;
int head[maxn],dis[maxn],dep[maxn],f[maxn][30],n,cnt;
struct node{
	int id;
	int val;
	int next;
}side[maxn*2];
void add(int x,int y,int d)
{
	side[cnt].id=y;
	side[cnt].val=d;
	side[cnt].next=head[x];
	head[x]=cnt++;
}
void init()
{
	memset(head,-1,sizeof(head));
	memset(dis,0,sizeof(dis));
	memset(f,0,sizeof(f));
	cnt=dep[1]=0;
}
void dfs(int x,int fa)
{
	for(int i=head[x];i!=-1;i=side[i].next)
	{
		int y=side[i].id;
		if(y==fa) continue;
		dep[y]=dep[x]+1;
		dis[y]=dis[x]+side[i].val;
		f[y][0]=x;
		dfs(y,x);
	}
}
void need()
{
	for(int j=1;(1<<j)<=n;j++)
		for(int i=1;i<=n;i++)
			f[i][j]=f[f[i][j-1]][j-1];
}
int lca(int a,int b)
{
	if(dep[a]>dep[b])
		swap(a,b);
	int ff=dep[b]-dep[a];
	for(int i=0;(1<<i)<=ff;i++)
		if((1<<i)&ff)
			b=f[b][i];
	if(a!=b)
	{
		for(int i=int(log2(n));i>=0;i--)
			if(f[a][i]!=f[b][i])
			{
				a=f[a][i];
				b=f[b][i];
			}
		a=f[a][0];
	}
	return a;
}
int main()
{
	int flag=0;
	while(~scanf("%d",&n))
	{
		if(flag)
			puts("");
		flag=1;
		init();
		for(int i=1;i<=n-1;i++)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			x++;y++;
			add(x,y,z);
			add(y,x,z);
		}
		dfs(1,-1);
		need();
		int q;
		scanf("%d",&q);
		while(q--)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			x++;y++;z++;
			int ans1=dis[x]+dis[y]-2*dis[lca(x,y)];
			int ans2=dis[y]+dis[z]-2*dis[lca(y,z)];
			int ans3=dis[x]+dis[z]-2*dis[lca(x,z)];
			printf("%d\n",(ans1+ans2+ans3)/2);
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值