1001: [BeiJing2006]狼抓兔子
Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 10707 Solved: 2464
Description
现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形:
左上角点为(1,1),右下角点为(N,M)(上图中N=4,M=5).有以下三种类型的道路 1:(x,y)<==>(x+1,y) 2:(x,y)<==>(x,y+1) 3:(x,y)<==>(x+1,y+1) 道路上的权值表示这条路上最多能够通过的兔子数,道路是无向的. 左上角和右下角为兔子的两个窝,开始时所有的兔子都聚集在左上角(1,1)的窝里,现在它们要跑到右下解(N,M)的窝中去,狼王开始伏击这些兔子.当然为了保险起见,如果一条道路上最多通过的兔子数为K,狼王需要安排同样数量的K只狼,才能完全封锁这条道路,你需要帮助狼王安排一个伏击方案,使得在将兔子一网打尽的前提下,参与的狼的数量要最小。因为狼还要去找喜羊羊麻烦.
Input
第一行为N,M.表示网格的大小,N,M均小于等于1000.接下来分三部分第一部分共N行,每行M-1个数,表示横向道路的权值. 第二部分共N-1行,每行M个数,表示纵向道路的权值. 第三部分共N-1行,每行M-1个数,表示斜向道路的权值. 输入文件保证不超过10M
Output
输出一个整数,表示参与伏击的狼的最小数量.
Sample Input
5 6 4
4 3 1
7 5 3
5 6 7 8
8 7 6 5
5 5 5
6 6 6
Sample Output
14
这道题虽然十分的裸,但其数据范围直接否定网络流的普通算法。
所以就要先求给定网格图的对偶图,然后在对偶图上跑最大流,将最小割问题转化为最短路问题。
奇妙吧!!!
代码:
program p1001;
const
maxn=500000;
type
rec=record
y,next,l:longint;
end;
var
a:array[0..6000000]of rec;
dist,d,g:array[0..2000000]of longint;
b:array[0..1000,0..1000,0..1]of longint;
v:array[0..2000000]of boolean;
i,j,m,n,ans,x,y,l,tot,sum,tt:longint;
procedure insert(x,y,l:longint);
begin
inc(tot);
a[tot].y:=y;
a[tot].l:=l;
a[tot].next:=g[x];
g[x]:=tot;
end;
procedure spfa;
var
t,w,i,j,x:longint;
begin
fillchar(dist,sizeof(dist),63);
d[0]:=0;t:=0;w:=0;v[0]:=true;dist[0]:=0;
while t<>w+1 do begin
x:=d[t];inc(t);v[x]:=false;
if t=maxn then t:=0;
i:=g[x];
while i<>0 do begin
j:=a[i].y;
if dist[x]+a[i].l<dist[j] then begin
dist[j]:=dist[x]+a[i].l;
if not v[j] then begin
if dist[j]<dist[d[t]] then begin
dec(t);
if t=-1 then t:=maxn-1;
d[t]:=j;
end
else begin
inc(w);
if w=maxn then w:=0;
d[w]:=j;
end;
v[j]:=true;
end;
end;
i:=a[i].next;
end;
end;
end;
begin
readln(m,n);
for i:=1 to m-1 do
for j:=1 to n-1 do begin
inc(sum);
b[i,j,0]:=sum;
inc(sum);
b[i,j,1]:=sum;
end;
tt:=sum+1;
for i:=1 to m do
for j:=1 to n-1 do begin
read(x);
if i=1 then insert(b[i,j,1],tt,x);
if i=m then insert(0,b[i-1,j,0],x);
if (i<>1)and(i<>m) then begin
insert(b[i,j,1],b[i-1,j,0],x);
insert(b[i-1,j,0],b[i,j,1],x);
end;
end;
for i:=1 to m-1 do
for j:=1 to n do begin
read(x);
if j=1 then insert(0,b[i,j,0],x);
if j=n then insert(b[i,j-1,1],tt,x);
if (j<>1)and(j<>n) then begin
insert(b[i,j,0],b[i,j-1,1],x);
insert(b[i,j-1,1],b[i,j,0],x);
end;
end;
for i:=1 to m-1 do
for j:=1 to n-1 do begin
read(x);
insert(b[i,j,0],b[i,j,1],x);
insert(b[i,j,1],b[i,j,0],x);
end;
spfa;
writeln(dist[tt]);
end.