hdu 2119 Matrix

ACM比赛整理

共 2254字,需浏览 5分钟

 · 2021-09-26

Matrix

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


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

 


矩阵


问题描述

给你一个矩阵(只包含0或1),每次你可以选择一行或一列,并删除所有的“1”在这行或这列。

你的任务是给出删除矩阵中所有'1'的最小次数。


输入

有几个测试用例。

第一行包含两个整数n,m(1<=n,m<=100), n是给定矩阵的行数,m是给定矩阵的列数。

接下来的n行描述矩阵:每一行包含m个整数,可以是' 1 '或' 0 '。

N =0表示输入结束。


输出

对于每个测试用例,按照输入中给出的顺序,打印一行包含删除矩阵中所有'1'的最小次数。


样例输入

3 3

0 0 0

1 0 1

0 1 0

0


样例输出

2


题意:

选择某一行或者某一列,删除一,使其变为0,问最少需要几次这样的操作才能把所有的元素变为0。

思路:对i,k建边,,所以,,

问题就转化为2分匹配,

也就是,最小顶点的覆盖,,

根据图论知识,最小定点覆盖等于最大匹配。。


代码:

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

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

int Find(int x)
{
int i;
for(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 1;
}
}
}
return 0;
}

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


浏览 6
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报