题意:给一个矩阵,(矩阵的值只有0和1)你可以划掉一行所有的1,问最少划多少次?
思路:二分图最小点覆盖的概念:假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖所有的边。行为X部,列为Y部,如果
aij
为
1
,就i,
j
连一条边。二分图最小点覆盖 == 二分图最大匹配。
http://acm.hdu.edu.cn/showproblem.php?pid=2119
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define rep(i,a,b) for(int i = a ; i <= b ; i ++)
#define rrep(i,a,b) for(int i = b ; i >= a ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x) memset(a,x,sizeof(a))
#define eps 1e-8
using namespace std;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 205;
const int MAXE = 10005;
typedef long long LL;
typedef unsigned long long ULL;
struct Edge {
int to;
Edge * next;
}E[MAXE],*EE;
struct Gragh {
Edge * first;
}G[MAXN];
int N,M;
bool visit[MAXN];
int match[MAXN];
void addedge(int u,int v) {
EE->to = v ; EE -> next = G[u].first ; G[u].first = EE ++;
}
int T,n,m,k;
void init() {
EE = E; N = M = 0;
cls(G,0);
}
bool find_path(int u) {
int v;
repE(p,u) {
v = p->to;
if(!visit[v]) {
visit[v] = 1;
if(match[v] == -1 || find_path(match[v])) {
match[v] = u;
return true;
}
}
}
return false;
}
int Max_match() {
cls(match,-1);
int cnt = 0;
rep(i,1,N) {
cls(visit,0);
if(find_path(i)) cnt ++;
}
return cnt;
}
void input() {
N = n ; M = m;
int tmp;
rep(i,1,n) rep(j,1,m) {
scanf("%d",&tmp);
if(tmp) addedge(i,N+j);
}
}
void solve() {
printf("%d\n",Max_match());
}
int main(void) {
while(scanf("%d",&n),n) {
scanf("%d",&m);
init();
input();
solve();
}
return 0;
}