拆点
把每个点拆成入口和出口两个点
以下几种连边方式就行了
-
对于有蜥蜴的点,(S)向入口连1的边
-
对于能出去的点,出口向汇点连容量为(inf)的边
-
每个点的入口和出口连容量为高度的边
-
之后能相互到达的点,出口向入口连容量为(inf)的边
代码
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 21
#define re register
#define LL long long
#define inf 99999999
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
inline int read()
{
char c=getchar();re int x=0;
while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
struct E{int v,nxt,w,f;} e[maxn*21*21*21];
int head[41*21],cur[41*21],d[41*21];
int a[maxn][maxn],b[maxn][maxn];
int n,num=1,S,T,D,m,tot;
char map[maxn][maxn],h[maxn][maxn];
inline int _(int x) { return (x>0)?x:-x;}
inline void add_edge(int x,int y,int z){e[++num].v=y;e[num].nxt=head[x];head[x]=num;e[num].w=z;}
inline void C(int x,int y,int z) {add_edge(x,y,z),add_edge(y,x,0);}
inline int BFS()
{
std::queue<int> q;
for(re int i=S;i<=T;i++) cur[i]=head[i],d[i]=0;d[S]=1;
q.push(S);
while(!q.empty())
{
int k=q.front();q.pop();
for(re int i=head[k];i;i=e[i].nxt)
if(!d[e[i].v]&&e[i].w>e[i].f) d[e[i].v]=d[k]+1,q.push(e[i].v);
}
return d[T];
}
int dfs(int x,int now)
{
if(x==T||!now) return now;
int flow=0,ff;
for(re int &i=cur[x];i;i=e[i].nxt)
if(d[e[i].v]==d[x]+1)
{
ff=dfs(e[i].v,min(now,e[i].w-e[i].f));
if(ff<=0) continue;
now-=ff,flow+=ff;
e[i].f+=ff,e[i^1].f-=ff;
if(!now) break;
}
return flow;
}
int main()
{
n=read(),m=read(),D=read();
for(re int i=1;i<=n;i++)
for(re int j=1;j<=m;j++) std::cin>>h[i][j];
for(re int i=1;i<=n;i++)
for(re int j=1;j<=m;j++) std::cin>>map[i][j];
for(re int i=1;i<=n;i++)
for(re int j=1;j<=m;j++) if(h[i][j]!='0') a[i][j]=++T;
for(re int i=1;i<=n;i++)
for(re int j=1;j<=m;j++) if(h[i][j]!='0') b[i][j]=++T;
++T,S=0;
for(re int i=1;i<=n;i++)
for(re int j=1;j<=m;j++)
if(h[i][j]!='0') C(a[i][j],b[i][j],h[i][j]-'0');
for(re int i=1;i<=n;i++)
for(re int j=1;j<=m;j++)
if(map[i][j]=='L'&&h[i][j]!='0') C(S,a[i][j],1),tot++;
for(re int i=1;i<=n;i++)
for(re int j=1;j<=m;j++)
{
if(h[i][j]=='0') continue;
for(re int k=1;k<=n;k++)
for(re int p=1;p<=m;p++)
if(_(i-k)+_(j-p)<=D&&h[k][p]!='0') C(b[i][j],a[k][p],inf);
}
for(re int i=1;i<=n;i++)
for(re int j=1;j<=m;j++)
if(_(i)<=D||_(j)<=D||i+D-1>=n||j+D-1>=m)
if(h[i][j]!='0') C(b[i][j],T,inf);
while(BFS()) tot-=dfs(S,inf);
printf("%d
",tot);
return 0;
}