1 /**************************************************************
2 Problem: 1001
3 User: 1090900715
4 Language: Pascal
5 Result: Accepted
6 Time:7744 ms
7 Memory:103760 kb
8 ****************************************************************/
9
10 program j01;
11 const maxn=2000086;
12 var b:array[0..1000,0..1000,0..1]of longint;
13 q,next,data:array[0..6000086]of longint;
14 dis,head,l:array[0..maxn]of longint;
15 inl:array[0..maxn]of boolean;
16 n,m,x,i,j,tt,tot:longint;
17
18 procedure add(u,v,w:longint);
19 begin
20 inc(tt);
21 q[tt]:=v;
22 next[tt]:=head[u];
23 head[u]:=tt;
24 data[tt]:=w;
25 end;
26
27 procedure spfa;
28 var h,tail,i,j:longint;
29 begin
30 fillchar(dis,sizeof(dis),$3f);
31 fillchar(inl,sizeof(inl),0);
32 dis[0]:=0;h:=0;tail:=1;l[1]:=0;inl[0]:=true;
33 while h<>tail do
34 begin
35 inc(h);if h>maxn then h:=1;
36 i:=l[h];
37 j:=head[i];
38 while j>0 do
39 begin
40 if dis[i]+data[j]<dis[q[j]] then
41 begin
42 dis[q[j]]:=dis[i]+data[j];
43 if inl[q[j]]=false then
44 begin
45 inc(tail);if tail>maxn then tail:=1;
46 l[tail]:=q[j];
47 inl[q[j]]:=true;
48 end;
49 end;
50 j:=next[j];
51 end;
52 inl[i]:=false;
53 end;
54 end;
55
56 begin
57 readln(n,m);
58 tot:=0;
59 for i:=1 to n-1 do
60 for j:=1 to m-1 do
61 begin
62 inc(tot);
63 b[i,j,0]:=tot;
64 inc(tot);
65 b[i,j,1]:=tot;
66 end;
67 inc(tot);
68 fillchar(head,sizeof(head),0);
69 tt:=0;
70 for i:=1 to n do
71 for j:=1 to m-1 do
72 begin
73 read(x);
74 if i=1 then add(b[i,j,1],tot,x);
75 if i=n then
76 add(0,b[i-1,j,0],x);
77 if (i<>1)and(i<>n) then
78 begin
79 add(b[i,j,1],b[i-1,j,0],x);
80 add(b[i-1,j,0],b[i,j,1],x);
81 end;
82 end;
83 for i:=1 to n-1 do
84 for j:=1 to m do
85 begin
86 read(x);
87 if j=1 then
88 add(0,b[i,j,0],x);
89 if j=m then add(b[i,j-1,1],tot,x);
90 if (j<>1)and(j<>m) then
91 begin
92 add(b[i,j,0],b[i,j-1,1],x);
93 add(b[i,j-1,1],b[i,j,0],x);
94 end;
95 end;
96 for i:=1 to n-1 do
97 for j:=1 to m-1 do
98 begin
99 read(x);
100 add(b[i,j,0],b[i,j,1],x);
101 add(b[i,j,1],b[i,j,0],x);
102 end;
103 spfa;
104 writeln(dis[tot]);
105 end.
106