题目
最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地。据了解,这块土地是一块矩形的区域,可以纵横划分为N×M块小区域。GDOI要求将这些区域分为商业区和工业区来开发。根据不同的地形环境,每块小区域建造商业区和工业区能取得不同的经济价值。更具体点,对于第i行第j列的区域,建造商业区将得到Aij收益,建造工业区将得到Bij收益。另外同种的区域连在一起可以得到额外的收益,即如果相邻有K块(显然K不超过4)同种类型的区域,则这块区域能增加k×Cij收益。经过Tiger.S教授的勘察,收益矩阵A,B,C都已经知道了。你能帮GDOI求出一个收益最大的方案么?
分析
这题和之前的那道圈地计划不同之处是,这里是同种的可以获得收益。
那么我们就不必黑白染色了,直接连边
——上下左右相邻的连一条容量为利益的边
源点向每个点连A的利益
汇点向每个点连B的利益
然后就是最小割了。
问题是为什么这样是对的?
我们设i和j相邻,即:
s->i,(ai)
i->j,(ci+cj)
j->t,(bj)
s->j(aj),
j->t(bj)
若我们割掉的是i->j(ci+cj),那么我们就一定不会同时割s-i,s->j,或者i->t,j->t
也就是我们一定会选得不同,因为我们不会割没有意义的边。
这里请大家画画图理解。
var
n,m,t,i,j,x,y,an,ans,k,nu:longint;
e:array[1..4,1..2] of longint=((1,0),(0,1),(-1,0),(0,-1));
c:array[1..100,1..100] of longint;
b,las,nex,f:array[0..100000] of longint;
d,dis:array[0..20000] of longint;
procedure insert(x,y,z,k:longint);
begin
inc(nu);b[nu]:=y;nex[nu]:=las[x];las[x]:=nu;f[nu]:=z;
inc(nu);b[nu]:=x;nex[nu]:=las[y];las[y]:=nu;f[nu]:=k;
end;
function get(x,y:longint):longint;begin exit((x-1)*m+y);end;
function bfs:boolean;
var p,l,r:longint;
begin
d[l]:=0;l:=0;r:=1;fillchar(dis,sizeof(dis),0);dis[0]:=1;
while l<r do begin
inc(l);p:=las[d[l]];
while p<>0 do begin
if (dis[b[p]]=0)and(f[p]>0) then begin
inc(r);d[r]:=b[p];dis[b[p]]:=dis[d[l]]+1;
end;p:=nex[p];
end;
end;
exit(dis[t]<>0);
end;
function min(l,r:longint):longint;
begin
if l<r then exit(l);exit(r);
end;
function ditch(x,y:longint):longint;
var p,o:longint;
begin
if x=t then exit(y);
p:=las[x];ditch:=0;
while p<>0 do begin
if (dis[b[p]]=dis[x]+1)and(f[p]>0) then begin
o:=ditch(b[p],min(y,f[p]));
if o>0 then begin dec(y,o);dec(f[p],o);inc(f[p xor 1],o);inc(ditch,o);end;
end;p:=nex[p];
end;
if ditch=0 then dis[x]:=-1;
end;
begin
readln(n,m);t:=n*m+1;nu:=1;
for i:=1 to n do for j:=1 to m do begin read(x);insert(0,get(i,j),x,0);an:=an+x;end;
for i:=1 to n do for j:=1 to m do begin read(x);insert(get(i,j),t,x,0);an:=an+x;end;
for i:=1 to n do for j:=1 to m do read(c[i,j]);
for i:=1 to n do for j:=1 to m do for k:=1 to 4 do begin
x:=i+e[k,1];y:=j+e[k,2];
if (x<1)or(y<1)or(x>n)or(y>m) then continue;
if (i+j)mod 2=0 then begin
an:=an+(c[i,j]+c[x,y]);
insert(get(i,j),get(x,y),c[i,j]+c[x,y],c[i,j]+c[x,y]);
end;
end;
while bfs do ans:=ans+ditch(0,maxlongint);
writeln(an-ans);
end.