#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
#define maxn 22222
#define INF 0xfffffff
int n,m,d,ans;
char st1[100][100],st2[100][100];
int st,ed,en;
int first[maxn],dis[maxn],q[maxn],tab[100][100];
struct node
{
int v,c,next;
}e[maxn];
bool bfs()
{
memset(dis,-1,sizeof(dis));
int head=1;
int tail=1;
tail++;
q[tail]=st;
dis[st]=0;
while(head<tail)
{
head++;
int u=q[head];
for(int i=first[u];i!=-1;i=e[i].next)
{
if(e[i].c==0) continue;
if(dis[e[i].v]!=-1) continue;
dis[e[i].v]=dis[u]+1;
if(ed==e[i].v) return true;
tail++;
q[tail]=e[i].v;
}
}
return false;
}
int dfs(int x,int mx)
{
int flow=0,f,ret=0;
if(x==ed||mx==0) return mx;
for(int i=first[x];i!=-1;i=e[i].next)
{
if(e[i].c==0) continue;
if(dis[e[i].v]!=dis[x]+1) continue;
f=dfs(e[i].v,min(mx,e[i].c));
if(f)
{
e[i].c-=f;
e[i^1].c+=f;
flow+=f;
mx-=f;
if(!mx) break;
}
}
if(flow==0) dis[x]=-1;
return flow;
}
int add(int a,int b,int c)
{
en++;
e[en].v=b;
e[en].c=c;
e[en].next=first[a];
first[a]=en;
en++;
e[en].v=a;
e[en].c=0;
e[en].next=first[b];
first[b]=en;
}
void init()
{
en=-1;
memset(first,-1,sizeof(first));
st=2*n*m+1;
ed=st+1;
}
int dinic()
{
int tmp=0,maxflow=0;
while(bfs())
{
while(tmp=dfs(st,INF)) maxflow+=tmp;
}
return maxflow;
}
bool judge(int x1,int y1,int x2,int y2)
{
if(((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))<=(d*d)&&tab[x1][y1]&&tab[x2][y2])return 1;
return 0;
}
void build()
{
for(int x1=1;x1<=n;x1++)
for(int y1=1;y1<=m;y1++)
for(int x2=x1-d;x2<=x1+d;x2++)
for(int y2=y1-d;y2<=y1+d;y2++)
if(judge(x1,y1,x2,y2)&&(x1!=x2||y1!=y2))
{
add((x1-1)*m+y1+m*n,(x2-1)*m+y2,INF);
}
}
int main()
{
scanf("%d%d%d",&n,&m,&d);
init();
for(int i=1;i<=n;i++)
{
scanf("%s",st1[i]+1);
for(int j=1;j<=m;j++)
{
tab[i][j]=st1[i][j]-'0';
if(tab[i][j])
{
add((i-1)*m+j,(i-1)*m+j+m*n,tab[i][j]);
}
}
}
for(int i=1;i<=n;i++)
{
scanf("%s",st2[i]+1);
for(int j=1;j<=m;j++)
{
if(st2[i][j]=='L')
{
add(st,(i-1)*m+j,1);
ans++;
}
}
}
for(int i=1;i<=d;i++)
{
for(int j=d+1;j<=n-d;j++)
{
add((j-1)*m+i+m*n,ed,INF);
add((j-1)*m+m-i+1+m*n,ed,INF);
}
}
for(int i=1;i<=d;i++)
{
for(int j=1;j<=m;j++)
{
add((i-1)*m+j+m*n,ed,INF);
add((n-i)*m+j+m*n,ed,INF);
}
}
build();
printf("%d\n",ans-dinic());
return 0;
}
bzoj1066: [SCOI2007]蜥蜴 最大流
最新推荐文章于 2018-01-17 16:41:13 发布