http://acm.hdu.edu.cn/showproblem.php?pid=2119

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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.
 
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.
 
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
 
 
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,a[101][101];
int tot,front[101],nxt[10010],to[10010];
int match[101],ans;
bool v[101];
void add(int u,int v)
{
    to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
}
bool go(int u)
{
    for(int i=front[u];i;i=nxt[i])
    {
        if(!v[to[i]])
        {
            v[to[i]]=true;
            if(!match[to[i]]||go(match[to[i]]))
            {
                match[to[i]]=u;
                return 1;
            }        
        }
    }
    return 0;
}
int main()
{
    while(scanf("%d",&n))
    {
        if(!n) return 0;
        scanf("%d",&m);
        ans=0; tot=0;
        memset(front,0,sizeof(front));
        memset(match,0,sizeof(match));
        for(int i=1;i<=n;i++)
         for(int j=1;j<=m;j++)
          {
              scanf("%d",&a[i][j]);
              if(a[i][j]) add(i,j);
          }
        for(int i=1;i<=n;i++)
         {
             memset(v,0,sizeof(v));
             if(go(i)) ans++;
          }
         printf("%d\n",ans);
    }

}

 

相关文章:

  • 2021-05-22
  • 2022-02-17
  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
  • 2022-02-07
  • 2022-03-01
猜你喜欢
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2021-11-21
  • 2021-07-14
  • 2021-05-26
相关资源
相似解决方案