HDU 5016 Mart Master II

2023-05-24,,

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的相关教程结束。

《HDU 5016 Mart Master II.doc》

下载本文的Word格式文档,以方便收藏与打印。