zjhu1023分房间

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=205;
int n,m,g[maxn][maxn],pre[maxn],flow[maxn];
bool bfs(int s,int t,int v){
	memset(pre,-1,sizeof(pre));
	memset(flow,inf,sizeof(flow));
	queue<int >q;
	q.push(s);
	pre[s]=0;
	while(!q.empty()){
		int now=q.front();
		if(now==t) break;
		for(int i=1;i<=v;i++)
			if(pre[i]==-1&&g[now][i]>0){
                pre[i]=now;
                flow[i]=min(flow[now],g[now][i]);
                //到当前点为止,路径上最小的流量
                q.push(i);
            }
		q.pop();
	}
	return pre[t]!=-1;

}
void update(int s,int t){
	for(int fa,now=t;now!=s;now=fa)
		fa=pre[now],g[fa][now]-=flow[t],g[now][fa]+=flow[t]; 
}
int main(){
	int t,maxflow;
	char str[maxn];
	cin>>t;
	while(t--){
		memset(g,0,sizeof(g));
		maxflow=0;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++){
			g[n+m+1][i]=1;
			scanf("%s",str+1);
			for(int j=1;j<=m;j++)
				g[i][n+j]=str[j]-'0';
		}
		for(int j=1;j<=m;j++)
			g[n+j][n+m+2]=1;
		while(bfs(n+m+1,n+m+2,n+m+2)){
			maxflow+=flow[n+m+2];
			update(n+m+1,n+m+2);
		}
		printf("%d\n",maxflow);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_50904510/article/details/120315528