BZOJ3459 : Bomb
二分答案,转化成判定所有科学家能否在lim时间内走到安全的地方
考虑网络流,对于每个非叶子节点,S向它连边,容量为该点科学家的人数
对于每个叶子节点,向T连边,容量为该点的容量
对于每个非叶子节点x,枚举它的所有祖先,对于一个祖先y,向y子树中所有与x距离不超过lim的点连边,容量为inf
由于数据随机,所以树的高度期望为$O(\log n)$
可以先对于每个点把该点子树内所有叶子节点按距离排序,然后第i小的点向第i-1小的点连边
每次查询时只要二分找到最大的满足条件的点然后向该点连边即可
如此建图,点数和边数均为$O(n\log n)$
#include<cstdio>
#include<algorithm>
const int N=10010,M=100010,inf=~0U>>2;
int n,i,j,x,y,sum,a[N],b[N],f[N],w[N],G[N],v[N],nxt[N],ed,dis[N];
int st[N],en[N],q[M],m,S,T,h[M],gap[M],maxflow,l,r=500000,mid,ans;
int g[M],d[M],cur;
struct E{int t,f,nxt;E(){}E(int _t,int _f,int _nxt){t=_t,f=_f,nxt=_nxt;}}e[1500000];
inline bool cmp(int x,int y){return dis[x]<dis[y];}
inline void addedge(int x,int y,int z){f[y]=x;w[y]=z;v[++ed]=y;nxt[ed]=G[x];G[x]=ed;}
void dfs2(int x){
if(!a[x])q[++m]=x;
for(int i=G[x];i;i=nxt[i])dfs2(v[i]);
}
void dfs(int x){
if(!a[x])return;
for(int i=G[x];i;i=nxt[i])dis[v[i]]=dis[x]+w[v[i]],dfs(v[i]);
st[x]=m+1;
for(int i=G[x];i;i=nxt[i])dfs2(v[i]);
en[x]=m;
std::sort(q+st[x],q+m+1,cmp);
}
inline int min(int x,int y){return x<y?x:y;}
inline void add(int s,int t,int f){
e[++cur]=E(t,f,g[s]);g[s]=cur;
e[++cur]=E(s,0,g[t]);g[t]=cur;
}
int sap(int v,int flow){
if(v==T)return flow;
int rec=0;
for(int p=d[v];p;p=e[p].nxt)if(h[v]==h[e[p].t]+1&&e[p].f){
int ret=sap(e[p].t,min(flow-rec,e[p].f));
e[p].f-=ret;e[p^1].f+=ret;d[v]=p;
if((rec+=ret)==flow)return flow;
}
if(!(--gap[h[v]]))h[S]=T;
gap[++h[v]]++;d[v]=g[v];
return rec;
}
bool check(int lim){
for(cur=i=1;i<=T;i++)g[i]=d[i]=h[i]=gap[i]=0;
for(i=n+1;i<=m;i++)add(i,q[i],inf);
for(i=1;i<=n;i++)if(a[i]){
add(S,i,b[i]);
for(j=i;j;j=f[j])if(dis[q[st[j]]]<=lim+dis[j]*2-dis[i]){
int l=st[j]+1,r=en[j],mid,t=st[j];
while(l<=r)if(dis[q[mid=(l+r)>>1]]<=lim+dis[j]*2-dis[i])l=(t=mid)+1;else r=mid-1;
add(i,t,inf);
}
for(j=st[i];j<en[i];j++)add(j+1,j,inf);
}else add(i,T,b[i]);
for(gap[maxflow=0]=T,i=1;i<=T;i++)d[i]=g[i];
while(h[S]<T)maxflow+=sap(S,inf);
return maxflow==sum;
}
int main(){
scanf("%d",&n);
for(i=1;i<=n;i++){
for(scanf("%d",&a[i]),j=0;j<a[i];j++)scanf("%d%d",&x,&y),addedge(i,x,y);
scanf("%d",&b[i]);
if(a[i])sum+=b[i];
}
for(m=n,i=1;i<=n;i++)if(!f[i])dfs(i);
S=m+1,T=S+1;
while(l<=r)if(check(mid=(l+r)>>1))r=(ans=mid)-1;else l=mid+1;
return printf("%d",ans),0;
}
BZOJ3459 : Bomb的更多相关文章
- HDU3555 Bomb[数位DP]
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submi ...
- Leetcode: Bomb Enemy
Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...
- HDU 5934 Bomb(炸弹)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 3622 Bomb Game(二分+2-SAT)
Bomb Game Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Bomb
Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro ...
- CF 363B One Bomb(枚举)
题目链接: 传送门 One Bomb time limit per test:1 second memory limit per test:256 megabytes Description ...
- hdu3555 Bomb (记忆化搜索 数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Memory ...
- [HDU3555]Bomb
[HDU3555]Bomb 试题描述 The counter-terrorists found a time bomb in the dust. But this time the terrorist ...
- hdu 5934 Bomb
Bomb Problem Description There are N bombs needing exploding.Each bomb has three attributes: explodi ...
随机推荐
- 用yaml来编写配置文件
yaml是一个数据序列化的标准,适用于所有开发语言,最大的特点是可读性好. yaml的一个主要应用方向就是编写配置文件,有非常多的系统和框架采用yaml进行配置. yaml有以下基本规则: 1.大小写 ...
- starUML的使用方法和各种线条的含义
使用方法https://www.cnblogs.com/syncCN/p/5433746.html 各种线条的含义:https://www.cnblogs.com/huaxingtianxia/p/6 ...
- android中实现在ImageView上随意画线涂鸦
我实现的思路: 1.继承ImageView类 2.重写onTouchEvent方法,在ACTION_MOVE(即移动时),记录下所经过的点坐标,在ACTION_UP时(即手指离开时,这时一条线已经画完 ...
- [整理]zepto的初次使用
http://www.css88.com/doc/zeptojs_api/ http://chaoskeh.com/blog/some-experience-of-using-zepto.html
- HDU 2988 Dark roads (裸的最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2988 解题报告:一个裸的最小生成树,没看题,只知道结果是用所有道路的总长度减去最小生成树的长度和. # ...
- sql 循环语句几种方式(变量循环,游标循环,事务)
--第一 ) ,),Orders )) declare @n int,@rows int --select @rows=count(1) from pe_Orders select @rows = ...
- 部分Web服务器信息对比
本文参考wikipedia的Web服务器比较页面的数据,选取了其中自己感兴趣的Web服务器的信息进行了对比,包括,Apache HTTP Server.Apache Tomcat.Nginx.Catt ...
- 文件时间戳修改touch和查看stat和ls --time
查看文件时间戳命令:stat awk.txtFile: `awk.txt'Size: 20 Blocks: 8 IO Block: 4096 regular fileDevice: 801h/2 ...
- CMT跟踪算法学习笔记(一)
关于这个算法,已经有网友写出笔记. 这位网友用画图的方式总结出了算法的各个流程,并总结了源码中的各个类:http://blog.csdn.net/roamer_nuptgczx/article/det ...
- [USACO17FEB]Why Did the Cow Cross the Road I G
一开始想写$DP$,发现直接转移完全有后效性 所以本小蒟蒻写了个最短路 每走三步就要吃草是这个题最难搞的地方,我们建图时不妨只对于距离等于三的点连边 考虑完全覆盖所有情况,从一个点走一步,两步,然后三 ...