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 ...
随机推荐
- arcengine,c# 二次开发
plication.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); ESRI.ArcGIS.Ru ...
- 在webconfig放置固定值
通常的,为了布置到服务器后修改的方便通常把一些会改变的值放在webconfig: 首先在web.ocnfig中放入如下值 <appSettings> <add key="A ...
- Objective-C Fast Enumeration
Fast enumeration is an Objective-C's feature that helps in enumerating through a collection. So in o ...
- MediaRecord一些使用记录
今天学习了MediaRecord的使用,第一次使用做个记录. MediaRecord作用是声音录制,使用步骤如下: 1.新建出音频文件代码如下: 先创建出用于存储音频文件 File dir = new ...
- -bash: mail: command not found
近日,安装了一个最小化的centos 6.3 6,用mail发送邮件进行测试的时候提示-bash: mail: command not found mailx没有安装,于是: yum -y insta ...
- java.lang.IllegalAccessException: Class XX can not access a member of class XXX with modifiers "private static"
当前需求: 利用反射获取某一属性值运行结果:java.lang.IllegalAccessException: Class com.example.demo.test.Reflect can not ...
- shell框架
#!/bin/bash#注释#注释#环境变量相关,如下PATH=/sbin:/bin:/usr/bin:/usr/sbin #引入库函数,如下,类似于c语言的#include "*.h&qu ...
- Win10系统64位快速专业安装版 V2016年
win10系统64位快速专业安装版 V2016年2月 系统下载:http://www.xitongma.com/ Ghost Win10 64位正式装机专业版2016 微软向Windows用户推送了w ...
- JavaScript实现的水果忍者游戏,支持鼠标操作
智能手机刚刚普及时,水果忍者这款小游戏可谓风靡一时.几年过去了,现在,让我们用纯JavaScript来实现这个水果忍者游戏,就算是为了锤炼我们的JavaScript开发技能吧. 大家可以通过这个链接在 ...
- UVA 427 The Tower of Babylon 巴比伦塔(dp)
据说是DAG的dp,可用spfa来做,松弛操作改成变长.注意状态的表示. 影响决策的只有顶部的尺寸,因为尺寸可能很大,所以用立方体的编号和高的编号来表示,然后向尺寸更小的转移就行了. #include ...