POJ1987——Distance Statistics

Distance Statistics
Time Limit: 2000MS Memory Limit: 64000K
Total Submissions: 1667 Accepted: 532
Case Time Limit: 1000MS

Description

Frustrated at the number of distance queries required to find a reasonable route for his cow marathon, FJ decides to ask queries from which he can learn more information. Specifically, he supplies an integer K (1 <= K <= 1,000,000,000) and wants to know how many pairs of farms lie at a distance at most K from each other (distance is measured in terms of the length of road required to travel from one farm to another). Please only count pairs of distinct farms (i.e. do not count pairs such as (farm #5, farm #5) in your answer).

Input

* Lines 1 ..M+1: Same input format as in "Navigation Nightmare"

* Line M+2: A single integer, K.

Output

* Line 1: The number of pairs of farms that are at a distance of at most K from each-other.

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
10

Sample Output

5

Hint

There are 5 roads with length smaller or equal than 10, namely 1-4 (3), 4-7 (2), 1-7 (5), 3-5 (7) and 3-6 (9).

Source

USACO 2004 February

求合法点对,树的点分治,和POJ1741差不多

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 40010;
const int M = 40010;

struct node
{
	int weight;
	int next;
	int to;
}edge[M << 1];

int tot, res, ans, n, m, k, root, size;
int head[N], num[N], dp[N];
bool vis[N];
int dist[N];

void addedge(int from, int to, int weight)
{
	edge[tot].weight = weight;
	edge[tot].to = to;
	edge[tot].next = head[from];
	head[from] = tot++;
}

void get_root(int u, int fa)
{
	dp[u] = 0;
	num[u] = 1;
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (v == fa || vis[v])
		{
			continue;
		}
		get_root(v, u);
		num[u] += num[v];
		dp[u] = max(dp[u], num[v]);
	}
	dp[u] = max(dp[u], size - num[u]);
	if (dp[root] > dp[u])
	{
		root = u;
	}
}


void calc_dist(int u, int d, int fa)
{
	dist[res++] = d;
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (v == fa || vis[v])
		{
			continue;
		}
		calc_dist(v, d + edge[i].weight, u);
	}
}

int calc(int u, int d)
{
	res = 0;
	calc_dist(u, d, -1);
	int ret = 0;
	sort(dist, dist + res);
	int i = 0, j = res - 1;
	while (i < j)
	{
		while (i < j && dist[i] + dist[j] > k)
		{
			j--;
		}
		ret += j - i;
		i++;
	}
	return ret;
}

void solve()
{
	ans += calc(root, 0);
	vis[root] = 1;
	for (int i = head[root]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (vis[v])
		{
			continue;
		}
		ans -= calc(v, edge[i].weight);
		root = 0;
		dp[0] = size = num[v];
		get_root(v, -1);
		solve();
	}
}

int main()
{
	int u, v, w;
	char dir[5];
	while (~scanf("%d%d", &n, &m))
	{ 
		memset( head, -1, sizeof(head) );
		memset ( num, 0, sizeof(num) );
		memset ( vis, 0, sizeof(vis) );
		tot = 0;
		ans = 0;
		root = 0;
		for (int i = 0; i < m; ++i)
		{
			scanf("%d%d%d%s", &u, &v, &w, dir);
			addedge(u, v, w);
			addedge(v, u, w);
		}
		scanf("%d", &k);
		dp[0] = size = n;
		get_root(1, -1);
		solve();
		printf("%d\n", ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值