const
maxn=2000009;
var
s,t,n,m,e:longint;
last,d,state:array[0..maxn] of longint;
side:array[1..maxn*3] of record
x,y,z,next:longint;
end;
v:array[0..maxn] of boolean;
flag:boolean;
procedure add(x,y,z:longint);
begin
inc(e);
side[e].x:=x; side[e].y:=y; side[e].z:=z;
side[e].next:=last[x]; last[x]:=e;
inc(e);
side[e].x:=y; side[e].y:=x; side[e].z:=z;
side[e].next:=last[y]; last[y]:=e;
end;
procedure init;
var
i,j,ans,x:longint;
begin
ans:=maxlongint;
readln(n,m);
flag:=true;
if (n=1)or(m=1) then flag:=false;
if (n=1)and(m=1) then
begin
writeln(0);
exit;
end;
if n=1 then
begin
for i:=1 to m-1 do
begin
read(x);
if x<ans then ans:=x;
end;
writeln(ans);
exit;
end;
if m=1 then
begin
for i:=1 to n-1 do
begin
read(x);
if x<ans then ans:=x;
end;
writeln(ans);
exit;
end;
s:=0;
t:=2*(n-1)*(m-1)+1;
for i:=1 to m-1 do
begin
read(x);
add(i*2,t,x);
end;
for i:=2 to n-1 do
for j:=1 to m-1 do
begin
read(x);
add(2*(i-1)*(m-1)+j*2,2*(i-2)*(m-1)+j*2-1,x);
end;
for i:=1 to m-1 do
begin
read(x);
add(s,2*(n-2)*(m-1)+i*2-1,x)
end;
for i:=1 to n-1 do
begin
read(x);
add(s,2*(i-1)*(m-1)+1,x);
for j:=2 to m-1 do
begin
read(x);
add(2*(i-1)*(m-1)+2*j-1,2*(i-1)*(m-1)+2*(j-1),x);
end;
read(x);
add(t,2*(i-1)*(m-1)+2*m-2,x);
end;
for i:=1 to n-1 do
for j:=1 to m-1 do
begin
read(x);
add(2*(i-1)*(m-1)+2*j-1,2*(i-1)*(m-1)+2*j,x);
end;
end;
procedure spfa;
var
head,tail,i,u:longint;
begin
fillchar(d,sizeof(d),$7f div 3);
d[s]:=0;
fillchar(v,sizeof(v),true);
v[s]:=false;
head:=0;
tail:=1;
state[1]:=s;
repeat
inc(head);
if head>t+7 then head:=1;
u:=state[head];
i:=last[u];
while i>0 do
with side[i] do
begin
if d[x]+z<d[y] then
begin
d[y]:=d[x]+z;
if v[y] then
begin
v[y]:=false;
inc(tail);
if tail>t+7 then tail:=1;
state[tail]:=y;
end;
end;
i:=next;
end;
v[u]:=true;
until head=tail;
writeln(d[t]);
end;
begin
init;
if flag then spfa;
end.
bzoj1001 spfa
最新推荐文章于 2018-12-11 20:05:47 发布