ZOJ - 3195 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

 

     就是求树上三点之间的最短路径,当然不能相加重复的路径。这样的话我们就可以将图简化成这样:

       这样对于第一种情况下:最短路径就是a1+a2+a3。怎么算这个a1+a2+a3呢。可以知道,相邻两个之间的最短路径分别是:a1+a2  a2+a3  a1+a3。这样的话所要的最短路径就是((a1+a2)+(a2+a3)+(a1+a3))/2。就是相邻两个之间的最短距离的和/2就是了。

      对于第二种情况下,我们按照之前的计算也是可以得到上述结论的。这样这题就好做了。

      同样,这个结论也可以拓展到更多点间的最短路径当中,结论自己yy一下就出来惹。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 50010;
const int DEG = 20;
struct fuck {
	int v, ne, w;
}ed[maxn << 1];
int head[maxn], cnt;
int fa[maxn][DEG];//父亲
int dep[maxn];//深度
long long dis[maxn];
void add(int u, int v, int w) {
	ed[cnt].v = v; ed[cnt].w = w;
	ed[cnt].ne = head[u]; head[u] = cnt++;
}
void init() {
	cnt = 0;
	memset(head, -1, sizeof(head));
}

void bfs(int root) {
	queue<int>q;
	dep[root] = 0;dis[root] = 0;
	fa[root][0] = root;
	q.push(root);
	while (!q.empty()) {
		int u = q.front();
		q.pop(); 
		for (int i = 1; i < DEG; i++) {
			fa[u][i] = fa[fa[u][i - 1]][i - 1];
		}
		for (int i = head[u]; ~i; i = ed[i].ne) {
			int v = ed[i].v;
			if (v == fa[u][0])continue;
			dep[v] = dep[u] + 1;
			dis[v] = dis[u] + ed[i].w;
			fa[v][0] = u;
			q.push(v);
		}
	}
}
int lca(int u, int v) {
	if (dep[u] > dep[v])swap(u, v);
	int hu = dep[u], hv = dep[v];
	int tu = u, tv = v;
	for (int det = hv - hu, i = 0; det; det >>= 1, i++) {
		if (det & 1)
			tv = fa[tv][i];
	}
	if (tu == tv)return tu;
	for (int i = DEG - 1; i >= 0; i--) {
		if (fa[tu][i] == fa[tv][i])continue;
		tu = fa[tu][i];
		tv = fa[tv][i];
	}
	return fa[tu][0];
}
long long di(int a, int b, int c) {
	return dis[a] + dis[b] - 2 * dis[c];
}
int main() {
	int n, m, q; int spot = 1;
	while (~scanf("%d",&n)) {
		if (!spot)puts("");
		else spot = 0;
		init();
		m = n - 1;
		while (m--) {
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			add(a, b, c);
			add(b, a, c);
		}
		int cnt = 0;
		bfs(0);
		scanf("%d", &q);
		while (q--) {
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			int ab = lca(a, b), bc = lca(b, c), ac = lca(c, a);
			cout << (di(a, b, ab) + di(a, c, ac) + di(b, c, bc)) / 2 << "\n";
		}
	}
	return 0;
}

       

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值