Matrix
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3116 Accepted Submission(s): 1441
Problem Description
Give you a matrix(only contains 0 or 1),every time you can select a row or a column and delete all the '1' in this row or this column .
Your task is to give out the minimum times of deleting all the '1' in the matrix.
Your task is to give out the minimum times of deleting all the '1' in the matrix.
Input
There are several test cases.
The first line contains two integers n,m(1<=n,m<=100), n is the number of rows of the given matrix and m is the number of columns of the given matrix.
The next n lines describe the matrix:each line contains m integer, which may be either ‘1’ or ‘0’.
n=0 indicate the end of input.
The first line contains two integers n,m(1<=n,m<=100), n is the number of rows of the given matrix and m is the number of columns of the given matrix.
The next n lines describe the matrix:each line contains m integer, which may be either ‘1’ or ‘0’.
n=0 indicate the end of input.
Output
For each of the test cases, in the order given in the input, print one line containing the minimum times of deleting all the '1' in the matrix.
Sample Input
3 3 0 0 0 1 0 1 0 1 0 0
Sample Output
2
Author
Wendell
Source
Recommend
威士忌
题意:给你一个01矩阵,每次可以将一行或一列的1全部变为0,问要多少次操作矩阵中的1能全部变为0
解题思路:二分图,最小覆盖点集
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n, m;
int x[525], y[525];
int s[525], nt[20050], e[20050];
int visit[525];
bool path(int k)
{
for (int i = s[k]; ~i; i = nt[i])
{
int ee = e[i];
if (!visit[ee])
{
visit[ee] = 1;
if (y[ee] == -1 || path(y[ee]))
{
y[ee] = k;
x[k] = ee;
return 1;
}
}
}
return 0;
}
void MaxMatch()
{
int ans = 0;
memset(x, -1, sizeof x);
memset(y, -1, sizeof y);
for (int i = 1; i <= n; i++)
{
if (x[i] == -1)
{
memset(visit, 0, sizeof visit);
if (path(i)) ans++;
}
}
printf("%d\n", ans);
}
int main()
{
while (~scanf("%d", &n) && n)
{
scanf("%d", &m);
int cnt = 1, k;
memset(s, -1, sizeof s);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
scanf("%d", &k);
if (k) nt[cnt] = s[i], s[i] = cnt, e[cnt++] = j;
}
MaxMatch();
}
return 0;
}