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 ...
随机推荐
- ps 进程管理
一. 进程管理 1. pstree 2. ps 3. top 4. nice 5. free 6. screen 二. 程序与进程 程序是静态的文件,进程是动态运行的程序. 三. 进程和线程 一个程序 ...
- jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法
jquery中ajax请求后台数据成功后既不执行success也不执行error,此外系统报错:Uncaught SyntaxError: Unexpected identifier at Objec ...
- arcgis jsapi接口入门系列(6):样式
symbol: function () { //线样式 //样式详情请看官方文档 let style = { //线颜色,支持多种格式: //CSS color string:例如"dodg ...
- PL/SQL学习笔记(四)之——删除重复记录
例:假设员工表中有若干记录重复,请删除重复的记录(某企业面试题) ------模拟建表 create table employee( e_id varchar2(20) primary key, e_ ...
- 重置Mysql的root密码及用户权限设置
一.重置Mysql的root密码 方法一: 直接进入localhost/phpmyadmin修改用户root的权限,设置密码: 方法二: 进入mysql控制台:mysql->use mysql ...
- selenium的定位
id定位 find_element_by_id()方法通过id来定位元素 例如: find_element_by_id("kw") find_element_by_id(&quo ...
- 刷新本地DNS缓存的方法
http://www.cnblogs.com/rubylouvre/archive/2012/08/31/2665859.html 常有人问到域名解析了不是即时生效的嘛,怎么还是原来的呢?答案就是在本 ...
- com.alibaba.dubbo.remoting.RemotingException: Failed to bind NettyServer on /192.168.1.13:20881, cause: Failed to bind to: /0.0.0.0:20881
抛出的异常如上,解决方案是:根据异常信息确定是端口被占用,排查项目是否启动之后没有关闭,在windows命令行中运行如下命令:netstat -ano 检查端口占用的情况,根据pid在任务管理器中杀死 ...
- CSS声明各个浏览器私有属性的命名前缀
-moz代表firefox浏览器私有属性-ms代表IE浏览器私有属性-webkit代表chrome.safari私有属性-o代表opera私有属性
- Jordan 标准型的推论
将学习到什么 从 Jordan 标准型出发,能够获得非常有用的信息. Jordan 矩阵的构造 Jordan 矩阵 \begin{align} J=\begin{bmatrix} J_{n_1}( ...