BZOJ 4554 [Tjoi2016&Heoi2016]游戏

题解:找出横着的联通块和纵着的联通块(能被炸弹一下炸到的)

如果一个炸弹能同时影响到某两个联通块就在联通块之间连边

这是一个二分图,求一下最大匹配

WoLeGeCao 50*50是2500不是250

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int oo=1000000000;
const int maxn=10009;

int n,m;
int ma[100][100];
char ss[100];

struct Edge{
	int from,to,cap,flow;
};
vector<int>G[maxn];
vector<Edge>edges;
void Addedge(int x,int y,int z){
	Edge e;
	e.from=x;e.to=y;e.cap=z;e.flow=0;
	edges.push_back(e);
	e.from=y;e.to=x;e.cap=0;e.flow=0;
	edges.push_back(e);
	int c=edges.size();
	G[x].push_back(c-2);
	G[y].push_back(c-1);
}

int s,t;
int vis[maxn];
queue<int>q;
int d[maxn];
int Bfs(){
	memset(vis,0,sizeof(vis));
	vis[s]=1;d[s]=0;q.push(s);
	while(!q.empty()){
		int x=q.front();q.pop();
		for(int i=0;i<G[x].size();++i){
			Edge e=edges[G[x][i]];
			if((e.cap>e.flow)&&(!vis[e.to])){
				vis[e.to]=1;
				d[e.to]=d[x]+1;
				q.push(e.to);
			}
		}
	}
	return vis[t];
}

int Dfs(int x,int a){
	if((a==0)||(x==t))return a;
	
	int nowflow=0,f;
	for(int i=0;i<G[x].size();++i){
		Edge e=edges[G[x][i]];
		if((d[x]+1==d[e.to])&&((f=Dfs(e.to,min(a,e.cap-e.flow)))>0)){
			nowflow+=f;
			a-=f;
			edges[G[x][i]].flow+=f;
			edges[G[x][i]^1].flow-=f;
			if(a==0)break;
		}
	}
	
	return nowflow;
}

int Maxflow(){
	int flow=0;
	while(Bfs())flow+=Dfs(s,oo);
	return flow;
}

int totn;
int Rnum[100][100];
int Lnum[100][100];

int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i){
		scanf("%s",ss+1);
		for(int j=1;j<=m;++j){
			if(ss[j]=='*')ma[i][j]=1;
			if(ss[j]=='X')ma[i][j]=2;
			if(ss[j]=='#')ma[i][j]=3;
		}
	}
	
	s=++totn;t=++totn;
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			if(ma[i][j]==3)continue;
			if(Rnum[i][j])continue;
			int k=j;
			Addedge(s,++totn,1);
			while(ma[i][k]!=3&&k<=m){
				Rnum[i][k]=totn;
				++k;
			}
		}
	}
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			if(ma[i][j]==3)continue;
			if(Lnum[i][j])continue;
			int k=i;
			Addedge(++totn,t,1);
			while(ma[k][j]!=3&&k<=n){
				Lnum[k][j]=totn;
				++k;
			}
		}
	}

	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			if(ma[i][j]!=1)continue;
			Addedge(Rnum[i][j],Lnum[i][j],1);
		}
	}
	
	printf("%d\n",Maxflow());
	return 0;
}

  

posted @ 2018-02-22 13:36  ws_zzy  阅读(142)  评论(0编辑  收藏  举报