练了这么久,应该就成了裸的最大流了吧
不1Y对不起自己
1 const inf=1000007; 2 type node=record 3 next,point,flow:longint; 4 end; 5 6 var edge:array[0..200010] of node; 7 a:array[0..30,0..30] of longint; 8 p,x,y,numh,h,cur,pre,f:array[0..1010] of longint; 9 len,ans,sum,t,tot,i,j,n,m,d:longint; 10 c:char; 11 12 function min(a,b:longint):longint; 13 begin 14 if a>b then exit(b) else exit(a); 15 end; 16 17 procedure add(x,y,f:longint); 18 begin 19 inc(len); 20 edge[len].point:=y; 21 edge[len].flow:=f; 22 edge[len].next:=p[x]; 23 p[x]:=len; 24 end; 25 26 procedure sap; 27 var u,q,i,j,tmp,neck:longint; 28 begin 29 u:=0; 30 numh[0]:=tot+1; 31 while h[0]tot do 37 begin 38 j:=cur[i]; 39 dec(edge[j].flow); 40 inc(edge[j xor 1].flow); 41 i:=edge[j].point; 42 end; 43 u:=0; 44 inc(ans); 45 end; 46 q:=-1; 47 i:=p[u]; 48 while i<>-1 do 49 begin 50 j:=edge[i].point; 51 if (edge[i].flow>0) and (h[u]=h[j]+1) then 52 begin 53 q:=i; 54 break; 55 end; 56 i:=edge[i].next; 57 end; 58 if q<>-1 then 59 begin 60 cur[u]:=q; 61 pre[j]:=u; 62 u:=j; 63 end 64 else begin 65 dec(numh[h[u]]); 66 if numh[h[u]]=0 then exit; 67 tmp:=tot+1; 68 i:=p[u]; 69 while i<>-1 do 70 begin 71 j:=edge[i].point; 72 if edge[i].flow>0 then tmp:=min(tmp,h[j]); 73 i:=edge[i].next; 74 end; 75 h[u]:=tmp+1; 76 inc(numh[h[u]]); 77 if u<>0 then u:=pre[u]; 78 end; 79 end; 80 end; 81 82 begin 83 readln(n,m,d); 84 len:=-1; 85 fillchar(p,sizeof(p),255); 86 for i:=1 to n do 87 begin 88 for j:=1 to m do 89 begin 90 read(c); 91 if c<>'0' then 92 begin 93 inc(t); 94 f[t]:=ord(c)-48; 95 x[t]:=i; 96 y[t]:=j; 97 a[i,j]:=t; 98 end; 99 end;100 readln;101 end;102 for i:=1 to n do103 begin104 for j:=1 to m do105 begin106 read(c);107 if c='L' then108 begin109 inc(sum);110 add(0,a[i,j],1);111 add(a[i,j],0,0);112 end;113 end;114 readln;115 end;116 tot:=t shl 1+1;117 for i:=1 to t do118 begin119 for j:=i+1 to t do120 if d*d>=sqr(x[i]-x[j])+sqr(y[i]-y[j]) then121 begin122 add(i+t,j,inf);123 add(j,i+t,0);124 add(j+t,i,inf);125 add(i,j+t,0);126 end;127 if (x[i]+d>n) or (x[i]-d<=0) or (y[i]+d>m) or (y[i]-d<=0) then128 begin129 add(i+t,tot,inf);130 add(tot,i+t,0);131 end;132 add(i,i+t,f[i]);133 add(i+t,i,0);134 end;135 sap;136 writeln(sum-ans);137 end.