HDU 5016 Mart Master II
Mart Master II
This problem will be judged on HDU. Original ID: 5016
64-bit integer IO format: %I64d Java class name: Main
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
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
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的更多相关文章
- HDU 5016 Mart Master II (树上点分治)
题目地址:pid=5016">HDU 5016 先两遍DFS预处理出每一个点距近期的基站的距离与基站的编号. 然后找重心.求出每一个点距重心的距离.然后依据dis[x]+dis[y] ...
- 【点分治】hdu5016 Mart Master II
点分治好题. ①手动开栈. ②dp预处理每个点被哪个市场控制,及其距离是多少,记作pair<int,int>数组p. ③设dis[u].first为u到重心s的距离,dis[u].seco ...
- 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 ...
- HDU 3081 Marriage Match II(二分法+最大流量)
HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- HDU 3081 Marriage Match II (二分图,并查集)
HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...
- HDU 3639 Bone Collector II(01背包第K优解)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 1023 Train Problem II
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Train Problem II Description As we all know the ...
- hdu 2639 Bone Collector II
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- HTML5+CSS3新增内容总结!!!!!绝对干货
说到H5C3会不会觉得东西好多啊,今天就整理了一份总结性的内容: CSS3选择器有哪些?答:属性选择器.伪类选择器.伪元素选择器.CSS3新特性有哪些?答:1.颜色:新增RGBA,HSLA模式 文字阴 ...
- MFC程序添加快捷键
[问题提出] 有的程序需要自定义组合键完成一定功能,如何实现? [解决方法] RegisterHotKey函数原型及说明: BOOL RegisterHotKey( H ...
- Nginx性能优化参考
nginx性能优化参考 1)调整配置文件中的配置项的值(配置文件:nginx.conf) worker_processes auto;开启的进程数,一般配置为跟逻辑CPU核数一样worker_rlim ...
- Django添加tinyMCE编辑器
tinymce的使用方法很简单,只需要在html页面中包含如下: <!-- Place inside the <head> of your HTML --> <scrip ...
- cookie安全
www.baidu.com host 文件 定义 a.baidu.com 为127.0.01 本地编写php程序 读取浏览器发送给 a.baidu.com的cookie 会把 .baidu.com域下 ...
- ios push Payload
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotifi ...
- WPF知识点全攻略10- 路由事件
路由事件是WPF不得不提,不得不会系列又一 先来看一下他的定义: 功能定义:路由事件是一种可以针对元素树中的多个侦听器(而不是仅针对引发该事件的对象)调用处理程序的事件. 实现定义:路由事件是一个 C ...
- k8s学习目录
目录 K8S基础部分 基础部分 5 秒创建 k8s 集群[转] k8s 核心功能[转] k8s 重要概念[转] 部署 k8s Cluster(上)[转] 部署 k8s Cluster(下)[转] Ku ...
- python_112_网络编程 Socket编程
实例1:客户端发小写英文,服务器端返回给客户端大写英文(仅支持一次接受发送) 服务器端: #服务器端(先于客户端运行) import socket server=socket.socket() ser ...
- Codeforces Round #271 (Div. 2)-B. Worms
http://codeforces.com/problemset/problem/474/B B. Worms time limit per test 1 second memory limit pe ...