HDU - 2586 How far away ? LCA

There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.

Input

First line is a single integer T(T<=10), indicating the number of test cases. 
  For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n. 
  Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.

Output

For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.

Sample Input

2
3 2
1 2 10
3 1 15
1 2
2 3

2 2
1 2 100
1 2
2 1

Sample Output

10
25
100
100

这是一道LCA的水题

但是我的spfa也过了(滑稽)

LCA:

#include<bits/stdc++.h>
const int maxn = 4e4 + 10;
int n, m, s;
int cnt;
int tot;
int head[maxn];
int depth[maxn * 2];
int order[maxn * 2];
int dis[maxn * 2]; 
int st[maxn * 2][15];
int fp[maxn * 2];
int lg[maxn * 2];
using namespace std;

struct node{
	int to;
	int next;
	int w;
	node() {}
	node(int a, int b, int c) : to(a), next(b), w(c) {}
}edge[maxn * 2];

void edgeadd(int a, int b, int c){

	edge[tot] = node(b, head[a], c); 
	head[a] = tot++;
	edge[tot] = node(a, head[b], c);

	head[b] = tot++;
}

void init(){
	memset(head, -1, sizeof(head));
	memset(depth, 0, sizeof(depth));
	memset(order, 0, sizeof(order));
	memset(dis, 0, sizeof(dis));
	memset(st, 0, sizeof(st));
	memset(fp, 0, sizeof(fp));
	tot = 0;
	cnt = 0;	
}

void dfs(int nw, int dpt){
	++cnt;
	fp[nw] = cnt;
	order[cnt] = nw;
	depth[cnt] = dpt + 1;
	for(int i = head[nw]; i != -1; i = edge[i].next){
		int v = edge[i].to;
		int w = edge[i].w;
		if(!fp[v]){
			dis[v] = dis[nw] + w;
			dfs(v, dpt + 1);
			++cnt;
			order[cnt] = nw;
			depth[cnt] = dpt + 1;		
		}
	}
}
void ST_init(){
	for(int i = 1; i <= cnt; i++)
		st[i][0] = i;
	int k = lg[cnt];
	for(int j = 1; j <= k; j++){
		for(int i = 1; i + (1 << j) - 1 <= cnt; i++){
			int a = st[i][j - 1];
			int b = st[i + (1 << (j - 1))][j - 1];
			if(depth[a] < depth[b])
				st[i][j] = a;
			else
				st[i][j] = b;

		}
	}
}

int main(){
	scanf("%d",&s);
	while(s--){
		init();
		scanf("%d%d",&n,&m);
		lg[0] = -1;
		for(int i = 1; i <= maxn * 2; i++)
			lg[i] = lg[i >> 1] + 1;
		for(int i = 1; i <= n - 1; i++){
			int a, b, c;
			scanf("%d%d%d",&a, &b, &c);
			edgeadd(a, b, c);
		}
		dfs(1, 0);
		ST_init();
		for(int i = 1; i <= m; i++){
			int l, r;
			scanf("%d%d",&l,&r);
			int a = fp[l];
			int b = fp[r];
			if(a > b)
				swap(a, b);
			int k = lg[b - a];
			int x = st[a][k];
			int y = st[b - (1 << k) + 1][k];
			int lcaans;
			if(depth[x] < depth[y])
				lcaans = order[x];
			else
				lcaans = order[y];
			int ans = dis[l] + dis[r] - (2 * dis[lcaans]);
			printf("%d\n",ans);
		}
		
	}
		
}

SPFA:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 4e4 + 10;
int tot;
int head[maxn], vis[maxn], dis[maxn];
int T;
int n, m;
int S, t;
struct node{
	int to;
	int next;
	int w;
	node() {}
	node(int a, int b, int c) : to(a), next(b), w(c) {} 
}edge[maxn * 2];
 
void init(){
	memset(head, -1, sizeof(head));
	memset(vis, 0, sizeof(vis));
	memset(dis, 0x3f3f3f3f, sizeof(dis));
	tot = 0;
}

void init1(){
	memset(vis, 0, sizeof(vis));
	memset(dis, 0x3f3f3f3f, sizeof(dis));
}
 
edgeadd(int a, int b, int c){
	edge[tot] = node(b, head[a], c);
	head[a] = tot++;
	edge[tot] = node(a, head[b], c);
	head[b] = tot++;
}
 
void spfa(int s){
	queue <int> q;
	q.push(s);
	vis[s] = 1;
	dis[s] = 0;
	while(!q.empty()){
		int nw = q.front();
		q.pop();
		vis[nw] = 0;
		for(int i = head[nw]; i != -1; i = edge[i].next){
			int v = edge[i].to;
			int w = edge[i].w;
			if(dis[v] > w + dis[nw]){
				dis[v] = w + dis[nw];
				if(!vis[v]){
					q.push(v);
					vis[v] = 1;
			}	
			}
		}
	}
	/*if(dis[t] == 0x3f3f3f3f)
		cout << "-1" << endl;
	else*/
		cout << dis[t] << endl;
}
 
int main(){
	//ios::sync_with_stdio(false);
	cin >> T;
	while(T--){
		init();
		scanf("%d%d",&n,&m);
		for(int i = 1; i <= n - 1; i++){
			int a, b, c;
			scanf("%d%d%d",&a, &b, &c);
			edgeadd(a, b, c);
		}
		for(int i = 1; i <= m; i++){
			int l, r;
			scanf("%d%d",&l,&r);
			init1();
			S = l;
			t = r;
			spfa(S);
		}
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值