#include<bits/stdc++.h>
using namespace std;
#define 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,maxf;
char str[maxn];
scanf("%d",&t);
while(t--){
memset(g,0,sizeof g);
maxf=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
g[n+m+1][i]=1;//源点n+m+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;//汇点n+m+2
while(bfs(n+m+1,n+m+2,n+m+2)){
maxf+=flow[n+m+2];
update(n+m+1,n+m+2);
}
printf("%d\n",maxf);
}
return 0;
}
网络流 分房间(最大流)
最新推荐文章于 2024-06-17 20:27:16 发布