bzoj 1189 二分+最大流判定
首先我们可以二分一个答案时间T,这样就将最优性问题
转化为了判定性问题。下面我们考虑对于已知的T的判定
对于矩阵中所有的空点bfs一次,得出来每个点到门的距离,
然后连接空点和每个能在t时间内到达的门一条边,容量为1,
之后连接源和每个空点一条边,容量为1,门连接汇边,容量为t。
判断最大流是否满流就好了。
/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/
//By BLADEVIL
type
rec =record
x, y :longint;
end;
var
n, m :longint;
pre, other, len, time :array[..] of longint;
last :array[..] of longint;
l :longint;
que1 :array[..] of rec;
dis :array[..,..] of longint;
go :array[..,..] of longint;
num :array[..,..] of longint;
source, sink :longint;
sum :longint;
map :array[..,..] of char;
que, d :array[..] of longint;
function min(a,b:longint):longint;
begin
if a>b then min:=b else min:=a;
end;
procedure connect(a,b,c,d:longint);
begin
inc(l);
pre[l]:=last[a];
last[a]:=l;
other[l]:=b;
len[l]:=c;
time[l]:=d;
end;
procedure make(a,b:longint);
var
h, t, curx, cury, nx, ny :longint;
i, j :longint;
f :boolean;
begin
connect(source,num[a,b],,);
connect(num[a,b],source,,);
fillchar(dis,sizeof(dis),);
dis[a,b]:=; que1[].x:=a; que1[].y:=b;
h:=; t:=;
while h<t do
begin
inc(h);
curx:=que1[h].x; cury:=que1[h].y;
for i:= to do
begin
nx:=curx+go[,i]; ny:=cury+go[,i];
if (nx<) or (nx>n) or (ny<) or (ny>m) then continue;
if dis[nx,ny]<> then continue;
if map[nx,ny]='X' then continue;
inc(t);
que1[t].x:=nx; que1[t].y:=ny;
dis[nx,ny]:=dis[curx,cury]+;
end;
end;
f:=false;
for i:= to n do
for j:= to m do
if map[i,j]='D' then
if dis[i,j]<> then
begin
f:=true;
connect(num[a,b],num[i,j],,dis[i,j]-);
connect(num[i,j],num[a,b],,dis[i,j]-);
end;
if not f then
begin
writeln('impossible');
halt;
end;
end;
procedure init;
var
i, j :longint;
begin
go[,]:=-; go[,]:=; go[,]:=; go[,]:=-;
readln(n,m);
for i:= to n do
begin
for j:= to m do read(map[i,j]);
readln;
end;
for i:= to n do
for j:= to m do num[i,j]:=(i-)*m+j;
source:=*n*m+; sink:=source+;
l:=;
for i:= to n do
for j:= to m do
if map[i,j]='.' then
begin
make(i,j);
inc(sum);
end;
for i:= to n do
for j:= to m do
if map[i,j]='D' then
begin
connect(num[i,j],sink,,);
connect(sink,num[i,j],,);
end;
end;
function bfs(up:longint):boolean;
var
q, p :longint;
h, t, cur :longint;
begin
fillchar(d,sizeof(d),);
que[]:=source; h:=; t:=;
d[source]:=;
while h<t do
begin
inc(h);
cur:=que[h];
q:=last[cur];
while q<> do
begin
if (len[q]>) and (time[q]<=up) then
begin
p:=other[q];
if (d[p]=) then
begin
inc(t);
que[t]:=p;
d[p]:=d[cur]+;
if p=sink then exit(true);
end;
end;
q:=pre[q];
end;
end;
exit(false);
end;
function dinic(x,flow,up:longint):longint;
var
tmp, rest :longint;
q, p :longint;
begin
rest:=flow;
if x=sink then exit(flow);
q:=last[x];
while q<> do
begin
p:=other[q];
if (len[q]>) and (time[q]<=up) and (rest>) and (d[x]=d[p]-) then
begin
tmp:=dinic(p,min(len[q],rest),up);
dec(rest,tmp);
dec(len[q],tmp);
inc(len[q xor ],tmp);
end;
q:=pre[q];
end;
exit(flow-rest);
end;
function judge(mid:longint):boolean;
var
q :longint;
tot :longint;
i :longint;
begin
q:=last[sink];
while q<> do
begin
len[q]:=;
len[q xor ]:=mid;
q:=pre[q];
end;
tot:=;
while bfs(mid) do
tot:=tot+dinic(source,maxlongint,mid);
for i:= to l do if i mod = then
begin
inc(len[i],len[i xor ]);
len[i xor ]:=;
end;
if tot<sum then exit(false) else exit(true);
end;
procedure main;
var
l, r, mid, ans :longint;
begin
l:=; r:=;
while l<=r do
begin
mid:=(l+r) div ;
if judge(mid) then
begin
ans:=mid;
r:=mid-;
end else l:=mid+;
end;
writeln(ans);
end;
begin
init;
main;
end.
bzoj 1189 二分+最大流判定的更多相关文章
- bzoj 1305 二分+最大流判定|贪心
首先我们二分一个答案mid,在判定是否能举办mid次,那么对于每个次我们可以用最大流根据是否满流(流量为n*mid)来判定,对于每个点我们拆成两个点,分别表示这个人要和他喜欢和不喜欢的人一起跳舞,那么 ...
- bzoj 1189 二分+最大流
题目传送门 思路: 先预处理出每个人到每扇门的时间,用门作为起点进行bfs处理. 然后二分时间,假设时间为x,将每扇门拆成1到x,x个时间点,表示这扇门有几个时间点是可以出去的.对于一扇门,每个时间点 ...
- BZOJ-1822 Frozen Nova 冷冻波 计(jie)算(xi)几何+二分+最大流判定+经典建图
这道逼题!感受到了数学对我的深深恶意(#‵′).... 1822: [JSOI2010]Frozen Nova 冷冻波 Time Limit: 10 Sec Memory Limit: 64 MB S ...
- BZOJ 1189 二分匹配 || 最大流
1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1155 Solved: 420[Submi ...
- uvalive 3231 Fair Share 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。
/** 题目: uvalive 3231 Fair Share 公平分配问题 链接:https://vjudge.net/problem/UVALive-3231 题意:有m个任务,n个处理器,每个任 ...
- poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点
题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...
- HDU3081 Marriage Match II —— 传递闭包 + 二分图最大匹配 or 传递闭包 + 二分 + 最大流
题目链接:https://vjudge.net/problem/HDU-3081 Marriage Match II Time Limit: 2000/1000 MS (Java/Others) ...
- HDU-3081-Marriage Match II 二分图匹配+并查集 OR 二分+最大流
二分+最大流: 1 //题目大意:有编号为1~n的女生和1~n的男生配对 2 // 3 //首先输入m组,a,b表示编号为a的女生没有和编号为b的男生吵过架 4 // 5 //然后输入f组,c,d表示 ...
- hdu4560 不错的建图,二分最大流
题意: 我是歌手 Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Subm ...
随机推荐
- 通过 XML HTTP 把文本文件载入 HTML 元素
新建一个.aspx文件 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="01-通 ...
- Asp.NET MVC 中使用 SignalR 实现推送功能
一,简介Signal 是微软支持的一个运行在 Dot NET 平台上的 html websocket 框架.它出现的主要目的是实现服务器主动推送(Push)消息到客户端页面,这样客户端就不必重新发送请 ...
- js执行过程
正如我们了解的一样,当我们书写了JS程序之后,打开浏览器,我们的代码就可以开始运行了(当然保证你的代码没有问题,才能按照你的预期进行执行).刚才说的是JS执行的一个大的环境,今天我们学习一下,JS在解 ...
- js日期格式化方法 dateFormatFn
var dateFormatFn=function(val,fmt){ var _this = new Date(val); console.log(_this,_this.getFullYear() ...
- web.xml中的url-pattern映射规则
Servlet和filter是J2EE开发中常用的技术,使用方便,配置简单.servlet和filter中的url-pattern有一些文章在里面的,总结了一些东西,以免遇到问题又要浪费时间. 一,s ...
- 使用sqoop将mysql数据导入到hadoop
hadoop的安装配置这里就不讲了. Sqoop的安装也很简单. 完成sqoop的安装后,可以这样测试是否可以连接到mysql(注意:mysql的jar包要放到 SQOOP_HOME/lib 下): ...
- ASP.NET MVC4学习笔记之Controller激活的扩展
一. 为什么要进行扩展 在前面的分析中,我们知道默认的Controller激活系统只能实例化无参构造函数的Controller类型,但在某些情况一下,我们希望某些服务的实例能够自动注入到Control ...
- ROS 端口IP映射 动态IP映射
chain=dstnat action=dst-nat to-addresses= protocol=tcp dst-address-type=local dst-port= log=no log-p ...
- .NET设计模式系列文章 from TerryLee
http://www.cnblogs.com/Terrylee/archive/2006/07/17/334911.html 最初写探索设计模式系列的时候,我只是想把它作为自己学习设计模式的读书笔记来 ...
- 12.python中的列表
先看列表是如何创建的: a = ['scolia', 123] b = list('scolia',123) 同样有两种创建方式,但一般用第一种. 列表和元祖最大的不同就是列表是可以修改的. 老规矩, ...