【BZOJ3504】危桥(最大流)

时间:2023-03-08 16:45:36

题意:见题面

思路:http://www.cnblogs.com/chenyushuo/p/5139556.html

必须交换b1,b2做第二次最大流的原因:

【BZOJ3504】危桥(最大流)

假如一个a1到b2的一个流和b1到a2的一个流分别经过了一个桥的两个端点(u,v),如图

【BZOJ3504】危桥(最大流)

我们就可以将a1到b2的流量改为经过u-v-a2-T到达T,同理将b1到a2的流量改为经过v-u-b2-T到达T,这样就说明这个流是没有问题的
在这种情况下将b1和b2交换后,流量显然不会有变化
如果一个a1到b2的一个流和b1到a2的一个流没有经过任何一个桥的两个端点,这个流显然不合法,且在b1和b2交换后,流量会减小
 var fan:array[..]of longint;
head,vet,next,gap,len,dis:array[..]of longint;
a:array[..,..]of char;
ch:string;
n,m,i,tot,x1,x2,y1,y2,s1,s2,src,source,s,j:longint;
flag:boolean; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; procedure add(a,b,c:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
len[tot]:=c;
head[a]:=tot; inc(tot);
next[tot]:=head[b];
vet[tot]:=a;
len[tot]:=;
head[b]:=tot;
end; function dfs(u,aug:longint):longint;
var e,v,t,val,flow:longint;
begin
if u=src then exit(aug);
e:=head[u]; flow:=; val:=s-;
while e<> do
begin
v:=vet[e];
if len[e]> then
begin
if dis[u]=dis[v]+ then
begin
t:=dfs(v,min(len[e],aug-flow));
len[e]:=len[e]-t;
len[fan[e]]:=len[fan[e]]+t;
flow:=flow+t;
if dis[source]>=s then exit(flow);
if aug=flow then break;
end;
val:=min(val,dis[v]);
end;
e:=next[e];
end;
if flow= then
begin
dec(gap[dis[u]]);
if gap[dis[u]]= then dis[source]:=s;
dis[u]:=val+;
inc(gap[dis[u]]);
end;
exit(flow);
end; function maxflow:longint;
var ans:longint;
begin
fillchar(gap,sizeof(gap),);
fillchar(dis,sizeof(dis),);
gap[]:=s; ans:=;
while dis[source]<s do ans:=ans+dfs(source,maxlongint);
exit(ans);
end; procedure build;
var i,j:longint;
begin
for i:= to n do
for j:= to n do
begin
if a[i,j]='O' then add(i,j,);
if a[i,j]='N' then add(i,j,maxlongint);
end;
end; begin
assign(input,'bzoj3504.in'); reset(input);
assign(output,'bzoj3504.out'); rewrite(output);
for i:= to do
if i mod = then fan[i]:=i+
else fan[i]:=i-;
while not eof do
begin
readln(n,x1,y1,s1,x2,y2,s2);
if n= then break;
inc(x1); inc(y1); inc(x2); inc(y2);
tot:=; for i:= to n+ do head[i]:=;
for i:= to n do
begin
readln(ch);
for j:= to n do a[i,j]:=ch[j];
end;
s:=n; source:=s+; src:=s+; s:=s+;
build;
add(source,x1,*s1);
add(source,x2,*s2);
add(y1,src,*s1);
add(y2,src,*s2);
flag:=true;
if maxflow<*(s1+s2) then flag:=false;
if flag then
begin
tot:=; for i:= to n+ do head[i]:=;
build;
add(source,x1,*s1);
add(source,y2,*s2);
add(y1,src,*s1);
add(x2,src,*s2);
if maxflow<*(s1+s2) then flag:=false;
end;
if flag then writeln('Yes')
else writeln('No');
end;
close(input);
close(output);
end.