Mart Master II

Time Limit: 6000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 5016
64-bit integer IO format: %I64d      Java class name: Main

Trader Dogy lives in city S, which consists of n districts. There are n - 1 bidirectional roads in city S, each connects a pair of districts. Indeed, city S is connected, i.e. people can travel between every pair of districts by roads.

In some districts there are marts founded by Dogy’s competitors. when people go to marts, they’ll choose the nearest one. In cases there are more than one nearest marts, they’ll choose the one with minimal city number.

Dogy’s money could support him to build only one new marts, he wants to attract as many people as possible, that is, to build his marts in some way that maximize the number of people who will choose his mart as favorite. Could you help him?

 

Input

There are multiple test cases. Please process till EOF.

In each test case:

First line: an integer n indicating the number of districts.

Next n - 1 lines: each contains three numbers bi, ei and wi, (1 ≤ bi,ei ≤ n,1 ≤ wi ≤ 10000), indicates that there’s one road connecting city bi and ei, and its length is wi.

Last line : n(1 ≤ n ≤ 105) numbers, each number is either 0 or 1, i-th number is 1 indicates that the i-th district has mart in the beginning and vice versa.

 

Output

For each test case, output one number, denotes the number of people you can attract, taking district as a unit.

 

Sample Input

5
1 2 1
2 3 1
3 4 1
4 5 1
1 0 0 0 1
5
1 2 1
2 3 1
3 4 1
4 5 1
1 0 0 0 0
1
1
1
0

Sample Output

2
4
0
1 解题:挺恶心的一道题
 #include <bits/stdc++.h>
using namespace std;
using PII = pair<int,int>;
const int maxn = ;
const int INF = ~0u>>;
PII d[][maxn];
struct arc {
int to,w,next;
arc(int x = ,int y = ,int z = -) {
to = x;
w = y;
next = z;
}
} e[maxn<<];
int head[maxn],sz[maxn],maxson[maxn],mart[maxn],tot,cnt,n;
int ans[maxn];
void add(int u,int v,int w) {
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
}
queue<int>q;
bool done[maxn];
void spfa() {
memset(done,false,sizeof done);
while(!q.empty()){
int u = q.front();
q.pop();
done[u] = false;
for(int i = head[u]; ~i; i = e[i].next){
PII tmp(d[][u].first + e[i].w,d[][u].second);
if(d[][e[i].to] > tmp){
d[][e[i].to] = tmp;
if(!done[e[i].to]){
done[e[i].to] = true;
q.push(e[i].to);
}
}
}
}
}
int dfs(int u,int fa){
sz[u] = ;
maxson[u] = ;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa || done[e[i].to]) continue;
dfs(e[i].to,u);
sz[u] += sz[e[i].to];
maxson[u] = max(maxson[u],sz[e[i].to]);
}
return sz[u];
}
int root(const int sum,int u,int fa){
int ret = u;
maxson[u] = max(maxson[u],sum - sz[u]);
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa || done[e[i].to]) continue;
int x = root(sum,e[i].to,u);
if(maxson[x] < maxson[ret]) ret = x;
}
return ret;
}
void update(int u,int w,int fa){
d[][cnt] = PII(w,u);
d[][cnt++] = PII(d[][u].first - w,d[][u].second);
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].to == fa || done[e[i].to]) continue;
update(e[i].to,w + e[i].w,u);
}
}
void calc(int u,int w,int sg){
cnt = ;
update(u,w,);
sort(d[],d[] + cnt);
for(int i = ; i < cnt; ++i){
if(mart[d[][i].second]) continue;
auto it = lower_bound(d[],d[] + cnt,d[][i]) - d[];
ans[d[][i].second] += (cnt - it)*sg;
}
}
void solve(int u){
int rt = root(dfs(u,),u,);
done[rt] = true;
calc(rt,,);
for(int i = head[rt]; ~i; i = e[i].next){
if(done[e[i].to]) continue;
calc(e[i].to,e[i].w,-);
}
for(int i = head[rt]; ~i; i = e[i].next){
if(done[e[i].to]) continue;
solve(e[i].to);
}
}
int main() {
int u,v,w;
while(~scanf("%d",&n)) {
memset(head,-,sizeof head);
int ret = tot = ;
for(int i = ; i < n; ++i) {
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
for(int i = ; i <= n; ++i) {
scanf("%d",mart + i);
if(mart[i]) {
d[][i] = PII(,i);
q.push(i);
} else d[][i] = PII(INF,);
ans[i] = ;
}
spfa();
solve();
for(int i = ; i <= n; ++i)
ret = max(ret,ans[i]);
printf("%d\n",ret);
}
return ;
}

HDU 5016 Mart Master II的更多相关文章

  1. HDU 5016 Mart Master II (树上点分治)

    题目地址:pid=5016">HDU 5016 先两遍DFS预处理出每一个点距近期的基站的距离与基站的编号. 然后找重心.求出每一个点距重心的距离.然后依据dis[x]+dis[y] ...

  2. 【点分治】hdu5016 Mart Master II

    点分治好题. ①手动开栈. ②dp预处理每个点被哪个市场控制,及其距离是多少,记作pair<int,int>数组p. ③设dis[u].first为u到重心s的距离,dis[u].seco ...

  3. hdu 5016 点分治(2014 ACM/ICPC Asia Regional Xi'an Online)

    Mart Master II Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  4. HDU 3081 Marriage Match II(二分法+最大流量)

    HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...

  5. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  6. HDU 3081 Marriage Match II (二分图,并查集)

    HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...

  7. HDU 3639 Bone Collector II(01背包第K优解)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. hdu 1023 Train Problem II

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...

  9. hdu 2639 Bone Collector II

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. mongodb 原子操作findAndModify

    原子操作模型数据findAndModify 推荐的方法,以保持原子将保留所有的相关信息,这些信息经常更新,一个文档中使用嵌入文档.这将确保所有的更新为一个单一文档是原子. 考虑下面的 products ...

  2. 用java代码写一个简单的网上购物车程序

    需求:1.写一个商品类,有商品编号.商品名称.商品分类.商品单价属性.2.写一个商品条目信息类,有商品和数量两个属性,有商品总价格方法. 3.写一个购物车类,有添加商品方法.查看订单信息,删除商品,修 ...

  3. springclould feign客户端添加全局参数

    用springclould feign作为调用服务的客户端,一般来说参数可以写在feignclient的方法参数里 有时需要所有feign请求都统一添加一些参数,例如token用于鉴权等,可以这样做: ...

  4. 获取SD卡下Android/data/下文件

    通常情况下多数应用程序都会将缓存的位置选择为 /sdcard/Android/data/<application package>/cache 这个路径.选择在这个位置有两点好处:第一,这 ...

  5. uvm_scoreboard——得分

    scoreboard 是验证平台很重要的一部分,因为,验证就是给激励,然后,检查结果.而scoreboard 就是肩负这检查结果的重任.测试用例能不能过,全由scoreboard说了算. A scor ...

  6. shell流程语句使用介绍

    1)使用if.case.read例子1:#!/bin/bash#读取终端输入的字符read -p "Please input a Number:" nn1=`echo $n|sed ...

  7. C#反射调用小DEMO

    程序集的源代码: namespace DesignMode { class IOCTest { public void TestO() { Console.WriteLine("O方法&qu ...

  8. RMQ求区间最大最小值

    #include<iostream> #include<cmath> #include<cstdio> #define N 50005 using namespac ...

  9. ping 不通。无法访问目标主机

    台式机 使用无线网卡  又登录了VPN 有时候访问不了局域网内的主机 解决方案  添加路由即可 window+R 打开运行 输入cmd  然后输入 route add 10.16.1.89 10.13 ...

  10. rpn网络结构再分析

    这是rpn网络train阶段的网络结构图 rpn_conv1之前的网络是特征提取层,也是和fast rcnn共享的层.rpn_conv1是一层1*1的卷积,这一层是单独为rpn网络多提取一层特征,这一 ...