BZOJ1066 蜥蜴

BZOJ1066 蜥蜴

题目传送门

题解

比较裸的一道网络流,我们先进行拆点,对于每一根柱子,从这根柱子的入点向出点连一条流量为这条柱子的高度的边,对于两根能够相互到达的柱子,连一条流量为\(inf\)的边,然后对于初始有蜥蜴的柱子,从源点向这根柱子连一条流量为1的边,对于所有能够跳出边界的柱子,连一条流量为\(inf\)的边。最后跑一边最大流即可。

code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
/*================Header Template==============*/
#define PAUSE printf("Press Enter key to continue..."); fgetc(stdin);
const int maxn=50;
const int N=1e4+500;
const int M=1e5+500;
const int inf=0x3f3f3f3f;
int n,m,d,cnt,S=0,T=801;
int mp[maxn][maxn],idx[maxn][maxn],lz[maxn][maxn];
struct edge {
	int to,nxt,w;
}E[M];
int tot=1;
char s[maxn];
int head[N],dis[N];
/*==================Define Area================*/
void addedge(int u,int v,int w) {
	E[++tot].to=v;E[tot].nxt=head[u];head[u]=tot;E[tot].w=w;
	E[++tot].to=u;E[tot].nxt=head[v];head[v]=tot;E[tot].w=0;
}

int Dis(int x,int y,int nx,int ny) {
	return abs(x-nx)+abs(y-ny);
}

bool Judge(int x,int y,int nx,int ny) {
	if(Dis(x,y,nx,ny)<=d&&mp[x][y]&&mp[nx][ny]) return 1;
	return 0;
}

void Build() {
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++) {
			for(int nx=i-d;nx<=i+d;nx++) {
				for(int ny=j-d;ny<=j+d;ny++) {
					if(Judge(i,j,nx,ny)&&(i!=nx||j!=ny)) addedge(idx[i][j]+400,idx[nx][ny],inf);
				}
			}
		}
	}
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=m;j++) {
			if(mp[i][j]) addedge(idx[i][j],idx[i][j]+400,mp[i][j]);
		}
	}
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=m;j++) {
			if(lz[i][j]) addedge(S,idx[i][j],1);
		}
	}
	for(int i=1;i<=d;i++) {
		for(int j=d+1;j<=n-d;j++) {
			addedge(idx[j][i]+400,T,inf);
			addedge(idx[j][m-i+1]+400,T,inf);
		}
	}
	for(int i=1;i<=d;i++) {
		for(int j=1;j<=m;j++) {
			addedge(idx[i][j]+400,T,inf);
			addedge(idx[n-i+1][j]+400,T,inf);
		}
	}
}

bool bfs() {
	queue<int>Q;
	memset(dis,-1,sizeof dis);
	Q.push(S);
	dis[S]=0;
	while(!Q.empty()) {
		int o=Q.front();
		Q.pop();
		for(int i=head[o];~i;i=E[i].nxt) {
			int to=E[i].to;
			if(dis[to]==-1&&E[i].w) {
				dis[to]=dis[o]+1;
				Q.push(to);
			}
		}
	}
	return dis[T]!=-1;
}

int dfs(int o,int flow) {
	if(o==T) return flow;
	int used=0,k;
	for(int i=head[o];~i;i=E[i].nxt) {
		int to=E[i].to;
		if(dis[to]==dis[o]+1&&E[i].w) {
			k=dfs(to,min(flow-used,E[i].w));
			E[i].w-=k;E[i^1].w+=k;
			used+=k;
			if(used==flow) return flow;
		}
	}
	if(!used) dis[o]=-1;
	return used;
}

int main() {
	memset(head,-1,sizeof head);
	read(n);read(m);read(d);
	cnt=0;
	for(int i=1;i<=n;i++) {
		scanf("%s",s+1);
		for(int j=1;j<=m;j++) {
			mp[i][j]=s[j]-'0';
			idx[i][j]=++cnt;
		}
	}
	int c=0;
	for(int i=1;i<=n;i++) {
		scanf("%s",s+1);
		for(int j=1;j<=m;j++) {
			lz[i][j]=(s[j]=='L');
			c+=lz[i][j];
		}
	}
	Build();
	int ans=c;
	while(bfs()) {
		ans-=dfs(S,0x3f3f3f3f);
	}
	printf("%d\n",ans);
	return 0;
}
/*
5 8 2
00000000
02000000
00321100
02000000
00000000
........
........
..LLLL..
........
........
*/
posted @ 2018-08-07 09:20  Apocrypha  阅读(92)  评论(0编辑  收藏  举报