明显的二分图最大独立点权集
ans=总点权-最小割(最大流)
1 const dx:array[1..4] of longint=(0,0,-1,1); 2 dy:array[1..4] of longint=(1,-1,0,0); 3 inf=10000007; 4 5 type node=record 6 next,flow,point:longint; 7 end; 8 9 var edge:array[0..2000010] of node; 10 d,p,cur,numh,h,pre:array[0..1010] of longint; 11 num:array[0..50,0..50] of longint; 12 len,i,j,x,y,n,t,s,k:longint; 13 14 procedure add(x,y,z:longint); 15 begin 16 inc(len); 17 edge[len].point:=y; 18 edge[len].flow:=z; 19 edge[len].next:=p[x]; 20 p[x]:=len; 21 end; 22 23 //省略sap 24 25 begin 26 len:=-1; 27 fillchar(p,sizeof(p),255); 28 readln(n); 29 t:=n*n+1; 30 for i:=1 to n do 31 for j:=1 to n do 32 begin 33 read(x); 34 inc(k); 35 num[i,j]:=k; 36 if (i+j) mod 2=0 then 37 begin 38 add(0,k,x); 39 add(k,0,0); 40 end 41 else begin 42 add(k,t,x); 43 add(t,k,0); 44 end; 45 s:=s+x; 46 end; 47 48 for i:=1 to n do 49 for j:=1 to n do 50 if (i+j) mod 2=0 then 51 begin 52 for k:=1 to 4 do 53 begin 54 x:=i+dx[k]; 55 y:=j+dy[k]; 56 if num[x,y]>0 then 57 begin 58 add(num[i,j],num[x,y],inf); 59 add(num[x,y],num[i,j],0); 60 end; 61 end; 62 end; 63 writeln(s-sap); 64 end.