hdu 2119 Matrix

Posted 日拱一卒 功不唐捐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 2119 Matrix相关的知识,希望对你有一定的参考价值。

Matrix

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);
    }

}

 

以上是关于hdu 2119 Matrix的主要内容,如果未能解决你的问题,请参考以下文章

HDU——2119 Matrix

HDU——T 2119 Matrix

Hdu 2119 Matrix

HDU-2119-Matrix(最大匹配)

二分图匹配入门专题1D - Matrix hdu2119最小顶点覆盖

Client / Server Interoperability Support Matrix for Different Oracle Versions (Doc ID 207303.1)(代码片段