0
点赞
收藏
分享

微信扫一扫

hdu 2119

倚然君 2023-08-15 阅读 102


Matrix

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2205    Accepted Submission(s): 975

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

//最小顶点覆盖=最大匹配 


#include <stdio.h>
#include <string.h>

int ma[110][110];
int n,m;
bool vis[110];
int link[110];

bool Find(int x)
{
    for(int i=0;i<m;i++)
    {
        if(!vis[i]&&ma[x][i])
        {
            vis[i]=1;
            if(link[i]==-1||Find(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    while(~scanf("%d",&n)&&n)
    {
        scanf("%d",&m);
        memset(link,-1,sizeof(link));
        for(int i=0;i<n;i++)
            for(int k=0;k<m;k++)
                scanf("%d",&ma[i][k]);
        int ans=0;
        for(int i=0;i<n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(Find(i))
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}




举报

相关推荐

0 条评论