zoj 3195 Design the city

/*
Design the city
Time Limit: 1 Second      Memory Limit: 32768 KB
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

*/

/*
ps:本题求得是,联通三个点的最短距离。由于图的结构是一个图,所以对于任意两点间,有且只有一条路径。
所以我们想到求三个点中任意两点间的距离,然后结果除以2即可。
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int Tmaxn = 50000+10;
const int Qmaxn = 70000+10;
int dist[Tmaxn],vist[Tmaxn],fat[Tmaxn],head_tree[Tmaxn],head_query[Tmaxn];
int N,M,cnt_tree,cnt_query;
int res[Qmaxn][4];
struct tree{
	int to,w,next;
}Tree[Tmaxn<<1];
struct query{
	int to,next;
	int id,num;
}Query[Qmaxn*6];
void Init(){
	cnt_query = cnt_tree = 0;
	for(int i = 0; i < N; i++){
		dist[i] = vist[i] = 0;
		head_tree[i] = head_query[i] = -1;
		fat[i] = i;
	}
}
void ADD_tree(int u,int v,int w){
	Tree[cnt_tree].to = v;
	Tree[cnt_tree].w = w;
	Tree[cnt_tree].next = head_tree[u];
	head_tree[u] = cnt_tree++;
}
void ADD_query(int u,int v,int id,int num){
	Query[cnt_query].to = v;
	Query[cnt_query].id = id;
	Query[cnt_query].num = num;
	Query[cnt_query].next = head_query[u];
	head_query[u] = cnt_query++;
}
int Find(int x){
	if(x != fat[x])
		x = Find(fat[x]);
	return x;
}
void Tarjan(int u,int father){
	for(int i = head_tree[u]; ~i; i = Tree[i].next){
		int v = Tree[i].to;
		if(!vist[v] && v!=father){
			dist[v] = dist[u]+Tree[i].w;
			Tarjan(v,u);
			fat[v] = u;
		}
	}
	vist[u] = 1;
	for(int i = head_query[u]; ~i; i = Query[i].next){
		int v = Query[i].to;
		if(vist[v]){
			int temp = Find(v);
			res[Query[i].id][Query[i].num] = dist[u]+dist[v]-2*dist[temp];
		}
	}
}
int main(){
	int u,v,w;
	int cas = 0;
	while(~scanf("%d",&N)){
		cas++;
		Init();
		for(int i = 1; i < N; i++){
			scanf("%d %d %d",&u,&v,&w);
			ADD_tree(u,v,w);
			ADD_tree(v,u,w);
		}
		scanf("%d",&M);
		for(int i = 0; i < M; i++){
			scanf("%d %d %d",&u,&v,&w);
			ADD_query(u,v,i,0);
			ADD_query(v,u,i,0);
			ADD_query(v,w,i,1);
			ADD_query(w,v,i,1);
			ADD_query(w,u,i,2);
			ADD_query(u,w,i,2);
		}
		Tarjan(0,-1);
		if(cas>1)
			puts("");
		for(int i = 0; i < M; i++)
			printf("%d\n",(res[i][0]+res[i][1]+res[i][2])/2);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值