Distance Statistics
 
 

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). 
 

题解:

  POJ 1741

  http://www.cnblogs.com/zxhl/p/5692688.html

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 4e4+, M = 1e2+, mod = 1e9+, inf = 1e9+;
typedef long long ll; int ans, n,m,root , t = ,K,siz[N],head[N],f[N],deep[N],d[N],allnode,vis[N];
struct edg{int to,next,v,w;}e[N * ];
void add(int u,int v,int w) {e[t].to=v;e[t].v=w;e[t].next=head[u];head[u]=t++;} void getroot(int x,int fa) {
siz[x] = ;
f[x] = ;
for(int i=head[x];i;i=e[i].next) {
int to = e[i].to;
if(to == fa || vis[to]) continue;
getroot(to,x);
siz[x] += siz[to];
f[x] = max(f[x] , siz[to]);
}
f[x] = max(f[x] , allnode - siz[x]);
if(f[x] < f[root]) root = x;
}
void getdeep(int x,int fa) {
if(d[x] <= K) deep[++deep[]]=d[x];
for(int i=head[x];i;i=e[i].next) {
int to = e[i].to;
if(to == fa || vis[to]) continue;
d[to] = d[x] + e[i].v;
getdeep(to,x);
}
}
int cal(int x,int now) {
d[x]=now;deep[] = ;
getdeep(x,);
sort(deep+,deep+deep[]+);
int all = ;
for(int l=,r=deep[];l<r;) {
if(deep[l]+deep[r] <= K) {all+=r-l;l++;}
else r--;
}
return all;
}
void work(int x) {
ans+=cal(x,);
vis[x] = ;
for(int i=head[x];i;i=e[i].next) {
int to = e[i].to;
if(vis[to]) continue;
ans-=cal(to,e[i].v);
allnode = siz[to];
root = ;
getroot(to,root);
work(root);
}
}
void init()
{
memset(head,,sizeof(head));
t = ;
ans = root = ;
memset(vis,,sizeof(vis));
}
int main()
{
while(~scanf("%d%d",&n,&m)) {
init();
for(int i=;i<n;i++) {
int a,b,c;char ch[];
scanf("%d%d%d%s",&a,&b,&c,ch);
add(a,b,c) , add(b,a,c);
}
scanf("%d",&K);
allnode=n;f[]=inf;
getroot(,);
work(root);
printf("%d\n",ans);
} }

最新文章

  1. Linux下用ftp更新web内容!
  2. unison+inotify-tools触发式双向自动同步
  3. weblogic管理2 - 创建并启动一个managed server
  4. extjs 表格可复制
  5. Javascript Array Distinct (array.reduce实现)
  6. 5.5 Function类型
  7. Storm基础知识
  8. MySQL之 ALTER vs CHANGE vs MODIFY COLUMN
  9. mysqldump指定编码导出数据
  10. Linux多线程实践(5) --Posix信号量与互斥量解决生产者消费者问题
  11. [Luogu 4135] 作诗
  12. mysql 基础sql语句
  13. Zabbix4.0.3解决中文乱码
  14. B-树(B树)详解
  15. django 配置media 存放调用 图片、图标等文件
  16. Hibernate_day04
  17. 20180226xlVbaGetStockData
  18. 关于JAVA项目中的常用的异常处理情况总结
  19. 024-linux中动态库libXXX.so
  20. mysql中char,varchar,text

热门文章

  1. (转)高性能网站架构之缓存篇—Redis集群搭建
  2. iOS CLLocationManager 定位
  3. Linux下安装Scala
  4. Maven 3.3.3 Win10环境下的使用实例(下)
  5. 谷歌 Uncaught SecurityError: Failed to execute &#39;replaceState&#39; on &#39;History 错误
  6. 穹举,迭代,while循环。
  7. BlacJack游戏
  8. Jquery网站下雪花的效果
  9. PHP try catch
  10. August 9th 2016, Week 33rd Tuesday