bzoj1066: [SCOI2007]蜥蜴

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1066

思路:很显然的点容量限制,那就拆点,从一个点向另一个点连点容量限制的边

S向有蜥蜴的点连1的边,两两可达的点连边,可以出去的就向会连边

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0};
const int maxn=1010,maxm=100010,inf=1e9;
using namespace std;
int n,m,d,h[25][25],map[25][25],cnt,S=maxn-2,T=maxn-1;char ch[25][25];
int pre[maxm],now[maxn],son[maxm],val[maxm],tot=1,head,tail,q[maxn],dis[maxn];
int id(int x,int y){return (x-1)*m+y;}
void add(int a,int b,int c){pre[++tot]=now[a],now[a]=tot,son[tot]=b,val[tot]=c;}
//void ins(int a,int b,int c){add(a,b,c),add(b,a,0);}
void ins(int a,int b,int c){add(a,b,c),add(b,a,0);}
int po(int x,int op){return x*2+op;}
double sqr(int x){return 1.0*x*x;}
bool check(int x1,int y1,int x2,int y2){return sqrt(sqr(x1-x2)+sqr(y1-y2))<=1.0*d;}

void build(int sx,int sy){
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)	
			if (i!=sx||j!=sy)
				if (check(sx,sy,i,j)&&h[i][j])
					ins(po(id(sx,sy),1),po(id(i,j),0),inf);
}

void init(){
	scanf("%d%d%d",&n,&m,&d);
	for (int i=1;i<=n;i++) scanf("%s",ch[i]+1);
	for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) h[i][j]=ch[i][j]-'0';
	for (int i=1;i<=n;i++) scanf("%s",ch[i]+1);
	for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) map[i][j]=(ch[i][j]=='L');
	//for (int i=1;i<=n;i++,puts("")) for (int j=1;j<=m;j++) printf("%d",map[i][j]);
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++){
			int idx=id(i,j);
			if (h[i][j]){
				ins(po(idx,0),po(idx,1),h[i][j]);
				if (i<=d||i>=n-d+1||j<=d||j>=m-d+1) ins(po(idx,1),T,inf);
				build(i,j);
			}
			if (map[i][j]) ins(S,po(idx,0),1),cnt++;//,printf("fuckpp %d %d\n",i,j)
		}
}

bool bfs(){
	memset(dis,-1,sizeof(dis));
	q[tail=1]=S,dis[S]=head=0;
	while (head!=tail){
		if (++head>maxm) head=1;
		int x=q[head];
		for (int y=now[x];y;y=pre[y]) if (val[y]&&dis[son[y]]==-1){
			if (++tail>maxm) tail=1;
			q[tail]=son[y],dis[son[y]]=dis[x]+1;
		}
	}
	return dis[T]>0;
}

int find(int x,int low){
	if (x==T) return low;
	int y,res=0;
	for (y=now[x];y;y=pre[y]){
		if (dis[son[y]]!=dis[x]+1||!val[y]) continue;
		int tmp=find(son[y],min(low,val[y]));
		val[y]-=tmp,val[y^1]+=tmp,res+=tmp,low-=tmp;
		if (!low) break;
	}
	if (!y) dis[x]=-1;
	return res;
}

void work(){
	int ans=0;
	while (bfs()) ans+=find(S,inf);
	//printf("%d\n",ans);
	printf("%d\n",cnt-ans);
}

int main(){init(),work();return 0;}
/*
5 8 2
00000000
02000000
00321100
02000000
00000000
........
........
..LLLL..
........
........




3 3 1
000
011
000
...
.LL
...
*/


posted @ 2016-01-19 09:30  orzpps  阅读(71)  评论(0编辑  收藏