hdu 2119 Matrix
Matrix
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1490 Accepted Submission(s): 663
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.
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.
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
Recommend
1 //31MS 272K 796 B C++ 2 /* 3 4 题意: 5 给出一个n*m的01矩阵,求最少的囊括1的行或列数 6 7 二分匹配: 8 匈牙利算法模板题,难点是怎么想到用二分匹配 9 其实是最小顶点覆盖问题... 10 11 */ 12 #include<stdio.h> 13 #include<string.h> 14 int g[105][105]; 15 int match[105]; 16 int vis[105]; 17 int n,m; 18 int dfs(int x) 19 { 20 for(int i=0;i<m;i++){ 21 if(!vis[i] && g[x][i]){ 22 vis[i]=1; 23 if(match[i]==-1 || dfs(match[i])){ 24 match[i]=x; 25 return 1; 26 } 27 } 28 } 29 return 0; 30 } 31 int hungary() 32 { 33 memset(match,-1,sizeof(match)); 34 int ans=0; 35 for(int i=0;i<n;i++){ 36 memset(vis,0,sizeof(vis)); 37 ans+=dfs(i); 38 } 39 return ans; 40 } 41 int main(void) 42 { 43 while(scanf("%d%d",&n,&m),n) 44 { 45 memset(g,0,sizeof(g)); 46 for(int i=0;i<n;i++) 47 for(int j=0;j<m;j++) 48 scanf("%d",&g[i][j]); 49 printf("%d\n",hungary()); 50 } 51 return 0; 52 }