POJ1986(Distance Queries)

Distance Queries

Description

Farmer John’s cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in “Navigation Nightmare”,followed by a line containing a single integer K, followed by K “distance queries”. Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ’s distance queries as quickly as possible!
Input

  • Lines 1…1+M: Same format as “Navigation Nightmare”

  • Line 2+M: A single integer, K. 1 <= K <= 10,000

  • Lines 3+M…2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
    Output

  • Lines 1…K: For each distance query, output on a single line an integer giving the appropriate distance.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6
Sample Output

13
3
36

思路

给定一张无向图,图的点数和无向边数不知道(暂定一个10w个点,10w条无向边),然后有m行每行给定两个点和距离,以及方位。(方位没个卵子用),之后有k行每行给定一对点问这两点之间的距离。倍增LCA求出两点的最近公共祖先,然后两点的距离就是dist[x] + dist[y] - 2*dist[lca],这道题还有个毛病没说图连不连通,这个到好解决直接上并查集。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const int maxn = 100005;
const int maxd = 19;
struct edge{
	int to;
	int w; 
	int next;
}e[maxn<<1];
int f[maxn][20];
int s[maxn];
int dep[maxn];
int dist[maxn];
int head[maxn];
int tot;
inline 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 clear_set()
{
	tot = 0;
	memset(dist,0,sizeof(dist));
	memset(head,-1,sizeof(head));
	memset(dep,0,sizeof(dep));
	memset(f,0,sizeof(f));
	for(int i = 0;i < maxn;i++){
		s[i] = i;
	}
}
int find_set(int x)
{
	if(x != s[x]){
		s[x] = find_set(s[x]);
	}
	return s[x];
}
inline void dfs(int x,int fx)
{
	dep[x] = dep[fx] + 1;
	f[x][0] = fx;
	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){
		if(e[i].to == fx)	continue;
		dist[e[i].to] = dist[x] + e[i].w;
		dfs(e[i].to,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,q;
	while(~scanf("%d%d",&n,&m)){
		clear_set();
		int x,y,z;
		char str[5];			//方位没用 
		for(int i = 0;i < m;i++){
			scanf("%d%d%d%s",&x,&y,&z,str);
			int fx = find_set(x);
			int fy = find_set(y);
			if(fx != fy)	s[fx] = fy;
			addedge(x,y,z);addedge(y,x,z);
		}
		for(int i = 1;i <= n;i++){
			if(!dep[i]){
				dfs(i,0);
			}
		}
		scanf("%d",&q);
		while(q--){
			scanf("%d%d",&x,&y);
			int fx = find_set(x);
			int fy = find_set(y);
			if(fx != fy){
				printf("0\n");
				continue;	
			}
			z = LCA(x,y);
			int ans = dist[x] + dist[y] - 2*dist[z];
			printf("%d\n",ans);
		}
	}
	return 0;
}

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值