ZOJ3195(Design the city)

题目传送门

Design the city

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

题意

给定一张无向图,每条边都有一个权重(没有重边),然后有m次询问,每次给定三个点,问连接这三个点所需的路径长度是多少。

思路

ST倍增 + LCA + 思维
之前只做过两点的距离,虽然也是ST倍增 + LCA的做法,这道题是求三点的距离。这题利用LCA计算出三个点两两距离的和再除以二,其实可以推广到k个点。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 50005;
const int maxd = 19;
struct edge{
	int to;
	int w;
	int next;
}e[maxn<<1];
int f[maxn][20];
int head[maxn];
bool vis[maxn];
int dist[maxn];
int dep[maxn];
int tot;
void clear_set()
{
	tot = 0;
	memset(head,-1,sizeof(head));
	memset(vis,false,sizeof(vis));
	memset(dist,0,sizeof(dist));
	memset(dep,0,sizeof(dep));
	memset(f,0,sizeof(f));
}
void addedge(int x,int y,int z)
{
	e[tot].to = y;
	e[tot].w = z;
	e[tot].next = head[x];
	head[x] = tot++;
}
void dfs(int x,int fx)
{
	dep[x] = dep[fx] + 1;
	f[x][0] = fx;vis[x] = true;
	for(int i = 1;i <= maxd;i++){
		f[x][i] = f[f[x][i-1]][i-1];
	} 
	for(int i = head[x];~i;i = e[i].next){
		int y = e[i].to;
		if(y != fx){
			dist[y] = dist[x] + e[i].w;
			dfs(y,x);
		}
	}
}
int LCA(int x,int y)
{
	if(dep[x] < dep[y])		swap(x,y);
	int d = dep[x] - dep[y];
	for(int i = 0;i <= maxd;i++){
		if(((1<<i)&d)){
			x = f[x][i];
		}
	}
	if(x == y)		return x;
	for(int i = maxd;i >= 0;i--){
		if(f[x][i] != f[y][i]){
			x = f[x][i];
			y = f[y][i];
		}
	}
	return f[x][0];
}
int main()
{
	int n,m,k = 0;
	while(~scanf("%d",&n)){
		clear_set();
		int x,y,z;
		for(int i = 0;i < n-1;i++){
			scanf("%d%d%d",&x,&y,&z);
			addedge(x+1,y+1,z);	addedge(y+1,x+1,z);
		} 
		for(int i = 1;i <= n;i++){
			if(!vis[i]){
				dfs(i,0);
			}
		}
		if(k++ != 0){		//注意格式
			printf("\n");
		}
		scanf("%d",&m);
		while(m--){
			scanf("%d%d%d",&x,&y,&z);
			x++;y++;z++;
			int t1 = LCA(x,y);
			int ans1 = dist[x] + dist[y] - 2*dist[t1];
			int t2 = LCA(x,z);
			int ans2 = dist[x] + dist[z] - 2*dist[t2];
			int t3 = LCA(y,z);
			int ans3 = dist[y] + dist[z] - 2*dist[t3];
			int ans = (ans1 + ans2 + ans3)/2;
			printf("%d\n",ans);
		}	
	}
	return 0;
}

愿你走出半生,归来仍是少年~

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值