BJOI[beijing]2006 狼抓兔子
[BeiJing2006]狼抓兔子
Time Limit: 15 Sec Memory Limit: 162 MB
Submit: 1787 Solved: 405
[Submit][Status][Discuss]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
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 6Sample Output
type
ji=^rec;
rec=record
data,v:longint;
next:ji;
end;
var
a:array[0..2000000] of ji;
v:array[0..2000000] of boolean;
d:array[0..2000000] of int64;
q:array[0..3000000] of longint;
h:array[0..1001,0..1001,0..1] of longint;
i,j,k,m,n,vv,vs,vt:longint;
procedure spfa;
var
p:ji;
s,t,head,tail,i,now:longint;
begin
fillchar(d,sizeof(d),30);
fillchar(v,sizeof(v),0);
fillchar(q,sizeof(q),0);
d[vs]:=0; v[vs]:=true; q[1]:=vs;
head:=1; tail:=1; s:=1; t:=1;
while s<=t do
begin
now:=q[head];
p:=a[now];
while p<>nil do
begin
i:=p^.data;
if d[i]>d[now]+p^.v then
begin
d[i]:=d[now]+p^.v;
if not v[i] then
begin
inc(tail); inc(t);
if tail>3000000 then tail:=1;
q[tail]:=i;
v[i]:=true;
end;
end;
p:=p^.next;
end;
inc(head); inc(s);
if head>3000000 then head:=1;
v[now]:=false;
end;
end;
procedure insert(x,y,w:longint);
var
p:ji;
begin
new(p); p^.data:=y; p^.v:=w; p^.next:=a[x]; a[x]:=p;
new(p); p^.data:=x; p^.v:=w; p^.next:=a[y]; a[y]:=p;
end;
begin
readln(n,m); vv:=0;
for i:=1 to n-1 do
for j:=1 to m-1 do
for k:=0 to 1 do
begin
inc(vv);
h[i,j,k]:=vv;
end;
fillchar(a,sizeof(a),0);
vs:=vv+1; vt:=vs+1;
for i:=1 to m-1 do
begin
read(k);
insert(vs,h[1,i,0],k);
end;
for i:=1 to n-2 do
for j:=1 to m-1 do
begin
read(k);
insert(h[i,j,1],h[i+1,j,0],k);
end;
for i:=1 to m-1 do
begin
read(k);
insert(h[n-1,i,1],vt,k);
end;
for i:=1 to n-1 do
begin
read(k); insert(h[i,1,1],vt,k);
for j:=1 to m-2 do
begin
read(k);
insert(h[i,j,0],h[i,j+1,1],k);
end;
read(k); insert(vs,h[i,m-1,0],k);
end;
for i:=1 to n-1 do
for j:=1 to m-1 do
begin
read(k);
insert(h[i,j,0],h[i,j,1],k);
end;
spfa;
writeln(d[vt]);
end.