8.15 D

来源:互联网 发布:九仙图翅膀进阶数据 编辑:程序博客网 时间:2024/06/09 20:39

                                               D - Matrix

 Give you a matrix(only contains 0 or1),every time you can select a row or a column and delete all the '1' in thisrow or this column . 

Your task is to give out the minimum times of deleting all the '1' in thematrix.

Input

There are several test cases. 

The first line contains two integers n,m(1<=n,m<=100), n is the number ofrows 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 beeither ‘1’ or ‘0’. 

n=0 indicate the end of input. 

Output

For each of the test cases, in the order given in theinput, print one line containing the minimum times of deleting all the '1' inthe matrix. 

Sample Input

3 3

0 0 0

1 0 1

0 1 0

0

Sample Output

2

 

题意:输入数字n,m,然后输入一个大小为m*n的矩阵,横坐标和纵坐标分别表示两个点,0代表这两点不连通,1代表连通,求最小顶点覆盖。


#include<stdio.h>#include<string.h>int a[1000][1000],macth[1000],book[1000];int m,n;int dfs(int s)//匈牙利算法{int i;for(i=0;i<m;i++){if(book[i]==0&&a[s][i]==1){book[i]=1;if(macth[i]==-1||dfs(macth[i])){macth[i]=s;return 1;}}}return 0;}int main(){int i,j,k,l,sum;while(scanf("%d",&n),n!=0){memset(a,0,sizeof(a));memset(macth,-1,sizeof(macth));scanf("%d",&m);for(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%d",&a[i][j]);sum=0;for(i=0;i<n;i++){memset(book,0,sizeof(book));if(dfs(i))sum+=1;}printf("%d\n",sum);}return 0;}



原创粉丝点击