Mart Master II

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 285    Accepted Submission(s): 94

Problem Description
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
 
Source
2014 ACM/ICPC Asia Regional Xi'an Online

题目解说:http://www.itnose.net/detail/6118094.html

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/11/1 22:13:28
File Name :6.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=100100;
struct Edge{
int next,to,w;
}edge[maxn*2];
int head[maxn],tot;
void init(){
tot=0;
memset(head,-1,sizeof(head));
}
inline void addedge(int u,int v,int w){
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].w=w;
head[u]=tot++;
}
int size[maxn],vis[maxn],fa[maxn],que[maxn];
int TT;
inline int getroot(int u){
int Min=maxn,root=0;
int l,r;
que[l=r=1]=u;
fa[u]=0;
for(;l<=r;l++)
for(int i=head[que[l]];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa[que[l]]||vis[v]==TT)continue;
que[++r]=v;
fa[v]=que[l];
}
for(l--;l;l--){
int x=que[l],Max=0;
size[x]=1;
for(int i=head[x];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa[x]||vis[v]==TT)continue;
Max=max(Max,size[v]);
size[x]+=size[v];
}
Max=max(Max,r-size[x]);
if(Max<Min){
Min=Max;root=x;
}
}
return root;
}
int ans[maxn];
pair<int,int> pp[maxn],np[maxn];
int dis[maxn],type[maxn];
inline void go(int u,int pre,int w,int tt){
int l,r;
que[l=r=1]=u;
fa[u]=pre;dis[u]=w;
for(;l<=r;l++)
for(int i=head[que[l]];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa[que[l]]||vis[v]==TT)continue;
que[++r]=v;
fa[v]=que[l];
dis[v]=dis[que[l]]+edge[i].w;
}
int cnt=0;
for(int i=1;i<=r;i++){
int x=que[i];
pp[cnt++]=make_pair(np[x].first-dis[x],np[x].second);
}
sort(pp,pp+cnt);
for(int i=1;i<=r;i++){
int x=que[i];
if(type[x])continue;
int id=lower_bound(pp,pp+cnt,make_pair(dis[x],x))-pp;
ans[x]+=(cnt-id)*tt;
}
}
void solve(int u){
int root=getroot(u);
vis[root]=TT;
go(root,0,0,1);
for(int i=head[root];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(vis[v]==TT)continue;
go(v,root,edge[i].w,-1);
}
for(int i=head[root];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(vis[v]==TT)continue;
solve(v);
}
}
bool ff[maxn];
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int n;
memset(vis,0,sizeof(vis));
TT=0;
while(~scanf("%d",&n)){
init();
int u,v,w;
for(int i=1;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
for(int i=1;i<=n;i++)scanf("%d",&type[i]);
queue<int> q;
for(int i=1;i<=n;i++)
if(type[i]){
np[i]=make_pair(0,i);
ff[i]=true;
q.push(i);
}
else {
np[i]=make_pair(INF,0);
ff[i]=false;
}
while(!q.empty()){
int u=q.front();
q.pop();
ff[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next){
v=edge[i].to;
pair<int,int> tmp=make_pair(np[u].first+edge[i].w,np[u].second);
if(tmp<np[v]){
np[v]=tmp;
if(!ff[v]){
ff[v]=1;
q.push(v);
}
}
}
}
TT++;
for(int i=1;i<=n;i++)ans[i]=0;
solve(1);
int ret=0;
for(int i=1;i<=n;i++)ret=max(ret,ans[i]);
cout<<ret<<endl;
}
return 0;
}

最新文章

  1. AngularJS中的Provider们:Service和Factory等的区别
  2. NC nc5.x报表设置合计行是否显示
  3. R中NA和NaN的区别
  4. Different Ways to Add Parentheses——Leetcode
  5. (转载)Mysql使用Describe命令判断字段是否存在
  6. C#变量命名规范
  7. 关于GNU软件的版本号命名规则
  8. [刷题]算法竞赛入门经典(第2版) 6-4/UVa439 6-5/UVa1600
  9. WPF: WpfWindowToolkit 一个窗口操作库的介绍
  10. [LeetCode] Merge Two Binary Trees 合并二叉树
  11. [Swift]LeetCode850. 矩形面积 II | Rectangle Area II
  12. Service的绑定过程
  13. Elasticsearch NEST 控制字段名称命名格式
  14. react - next.js 设置body style
  15. 关于深度学习中的batch_size
  16. oracle instantclient_11_2 配置文件tnsnames.ora
  17. Gym-100883F、Gym-101095B状态压缩小结
  18. SMTP 通过 ssh 通道发送垃圾邮件
  19. .net core 2.2 部署CentOS7(5)部署.net core mvc
  20. js点击事件在苹果端失效的问题

热门文章

  1. 金蝶盘点机条码数据採集器PDA,WIFI已经连接,可是PDA应用程序还是网络初始化不成功?
  2. POJ 3481 &amp;amp; HDU 1908 Double Queue (map运用)
  3. JDBC使用数据库来完成分页功能
  4. 在github 网页上,删除已经建好的库
  5. sed正则表达式
  6. 基于visual Studio2013解决面试题之0206hash表实现
  7. list view Item 里面有ImageButton
  8. [Android Studio 权威教程]Windows下安装Android Studio
  9. hdu 4507 数位dp(求和,求平方和)
  10. 华为OJ培训主题 比赛统计