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 ...
随机推荐
- Query for Component Path within PeopleSoft Portal
1) Run the below SQL to get the content reference name for your component ;-- Replace :1 with the c ...
- Dev的DocumentManager 相关问题
1.改变DocumentManager包含的窗体的排列方式 if (this.documentManager1.View.Type != ViewType.NativeMdi) { this.docu ...
- Linux Shell脚本编程的注意事项
Linux下(Shell脚本 http://www.jbxue.com/jb/shell/)编程的一些注意事项,如编程风格.命名风格等. 一.常用技巧 ssh user@server bash < ...
- 【easyui】—easyui教你编写一个前台的架子
以前做项目都是在别人搭建好的环境下直接编写单独的页面,也没有处理过怎么搭建一个框架.看到别人的布局都挺好的,自己也想做一个走一下流程. 嘿,刚开始时看着别人写的代码,去找怎么写. 这是我自己的想法,使 ...
- 浅谈Objective-C异常处理
-----<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培 ...
- LN : leetcode 292 Nim Game
lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...
- 内核同步机制 RCU
Evernote分享地址:http://www.evernote.com/shard/s133/sh/8807320d-f54d-4e90-a31b-e2a3d35509ee/7539dc3931b8 ...
- hdu 2648 Shopping
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2648 纯暴力的方法T_T... 如下: #include<cstdio> #include ...
- JavaScript高级程序设计之表单基础
A FORM <form id='form' action='http://a-response-url' method="post"> <!--maxlengt ...
- java 切换
Android L之后推荐使用JDK7编译程序,这是自然发展规律,就像是4年前编译Android 1.6需要使用JDK5一样. 多版本JDK是可以共存的,只需要使用update-alternative ...