码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2119

时间:2015-08-09 20:42:22      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 2119

标签:

原文地址:http://blog.csdn.net/a73265/article/details/47379447

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!