题目描述
给定\(n\)个人和\(m\)个房间,每个房间最多只能住一个人。
再给定\(g[i][j]\)表示第\(i\)个人是否愿意住在第\(j\)个房间。
问最多能住下多少人。
输入
第一行包含一个正整数\(T(1\leq T\leq 10)\),表示测试数据的组数。
每组数据第一行包含两个正整数\(n,m(1\leq n,m\leq 50)\)。
接下来\(n\)行,每行一个长度为\(m\)的01串,如果第\(i\)行第\(j\)个字符为'1',说明第\(i\)个人愿意住在第\(j\)个房间,否则说明不愿意。
输出
对于每组数据输出一行一个整数,即最多能住下的人数。
简单的最大流使用ISAP板子
#include<bits/stdc++.h>
using namespace std;
typedef vector<string> VS;
const int N = 110,INF = 1e9;
struct Edge {
int from, to, cap, flow;
Edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) {}
};
bool operator < (const Edge& a, const Edge& b) {
return a.from < b.from || (a.from == b.from && a.to < b.to);
}
struct ISAP {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[N];
bool vis[N];
int d[N],cur[N],p[N],num[N];
void AddEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(t);
vis[t] = 1;
d[t] = 0;
while (!Q.empty()) {
int x = Q.front();
Q.pop();
for (int i = 0; i < G[x].size(); i++) {
Edge& e = edges[G[x][i] ^ 1];
if (!vis[e.from] && e.cap > e.flow) {
vis[e.from] = 1;
d[e.from] = d[x] + 1;
Q.push(e.from);
}
}
}
return vis[s];
}
void init(int n) {
this->n = n;
for (int i = 0; i < n; i++) G[i].clear();
edges.clear();
}
int Augment() {
int x = t, a = INF;
while (x != s) {
Edge& e = edges[p[x]];
a = min(a, e.cap - e.flow);
x = edges[p[x]].from;
}
x = t;
while (x != s) {
edges[p[x]].flow += a;
edges[p[x] ^ 1].flow -= a;
x = edges[p[x]].from;
}
return a;
}
int Maxflow(int s, int t) {
this->s = s;
this->t = t;
int flow = 0;
BFS();
memset(num, 0, sizeof(num));
for (int i = 0; i < n; i++) num[d[i]]++;
int x = s;
memset(cur, 0, sizeof(cur));
while (d[s] < n) {
if (x == t) {
flow += Augment();
x = s;
}
int ok = 0;
for (int i = cur[x]; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if (e.cap > e.flow && d[x] == d[e.to] + 1) {
ok = 1;
p[e.to] = G[x][i];
cur[x] = i;
x = e.to;
break;
}
}
if (!ok) {
int m = n - 1;
for (int i = 0; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if (e.cap > e.flow) m = min(m, d[e.to]);
}
if (--num[d[x]] == 0) break;
num[d[x] = m + 1]++;
cur[x] = 0;
if (x != s) x = edges[p[x]].from;
}
}
return flow;
}
};
ISAP FG;
void solve()
{
int n,m;
cin>>n>>m;
VS g(n);
for(int i=0;i<n;++i) cin>>g[i];
int s = 0,t = n+m+1;
//注意初始化
FG.init(n+m+2);
//从源点向所有的人连一条权值为1的边表示该人有房间或无房间
for(int i=1;i<=n;++i) FG.AddEdge(s,i,1);
//若人和房间匹配则加一条权值为1的边
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
if(g[i][j]=='1') FG.AddEdge(i+1,j+1+n,1);
//从房间向汇点连一条权值为一的边表示该房间有人住
for(int i=1;i<=m;++i) FG.AddEdge(n+i,t,1);
//一条S->T的路径表示一个人选择了一个房间
cout<<FG.Maxflow(s,t)<<'\n';
}
int main()
{
int T;
cin>>T;
while(T--)
{
solve();
}
}
使用hopcroft复杂度\(\sqrt{n}m\)
#include<bits/stdc++.h>
using namespace std;
typedef vector<string> VS;
const int N = 110;
const int INF = 1e9;
struct Hopcroft_Carp {
int nx, ny, e, mx[N], my[N], dx[N], dy[N], dis;
bool used[N];
std::vector<int> G[N];
void init(int nx,int ny)
{
this->nx = nx;
this->ny = ny;
for(int i = 1; i<=nx; ++i) G[i].clear();
memset(mx,0,sizeof mx);
memset(my,0,sizeof my);
}
void AddEdge(int u,int v)
{
G[u].push_back(v);
}
bool BFS() {
std::queue<int> Q;
dis = INF;
memset(dx, -1, sizeof dx);
memset(dy, -1, sizeof dy);
for (int i = 1; i <= nx; ++ i) {
if (mx[i] == 0) Q.push(i), dx[i] = 0;
}
while (!Q.empty())
{
int u = Q.front(); Q.pop();
if (dx[u] > dis) break;
int size = G[u].size();
for (int i = 0; i < size; ++ i)
{
int v = G[u][i];
if (dy[v] == -1)
{
dy[v] = dx[u] + 1;
if (my[v] == 0)
dis = dy[v];
else
dx[my[v]] = dy[v] + 1, Q.push(my[v]);
}
}
}
return dis != INF;
}
bool DFS(int u) {
int size = G[u].size();
for (int i = 0; i < size; ++ i) {
int v = G[u][i];
if (!used[v] && dy[v] == dx[u] + 1) {
used[v] = 1;
if (my[v] != 0 && dy[v] == dis) continue;
if (my[v] == 0 || DFS(my[v])) {
my[v] = u, mx[u] = v;
return 1;
}
}
}
return 0;
}
int Maxmatch() {
int ans = 0;
while (BFS()) {
memset(used, 0, sizeof used);
for (int i = 1; i <= nx; ++ i)
if (mx[i] == 0 && DFS(i)) ++ ans;
}
return ans;
}
}HC;
void solve()
{
int n,m;
cin>>n>>m;
HC.init(n,m);
VS g(n);
for(int i=0;i<n;++i) cin>>g[i];
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
if(g[i][j]=='1') HC.AddEdge(i+1,j+1);
cout<<HC.Maxmatch()<<'\n';
}
int main()
{
int T;
cin>>T;
while(T--)
{
solve();
}
}
标签:最大,int,房间,flow,edges,dy,Edge,size
From: https://www.cnblogs.com/ruoye123456/p/18370432