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;
}
本文探讨了一个关于二部图匹配的问题——D-Matrix最小顶点覆盖问题。通过使用匈牙利算法,文章提供了一种有效的解决方案来找到删除矩阵中所有1所需的最小次数。
17

被折叠的 条评论
为什么被折叠?



