8.15 D - Matrix

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

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



Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 26354 (\N{CJK UNIFIED IDEOGRAPH-66F2}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 38754 (\N{CJK UNIFIED IDEOGRAPH-9762}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 25195 (\N{CJK UNIFIED IDEOGRAPH-626B}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 25551 (\N{CJK UNIFIED IDEOGRAPH-63CF}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 36335 (\N{CJK UNIFIED IDEOGRAPH-8DEF}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 24452 (\N{CJK UNIFIED IDEOGRAPH-5F84}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 35268 (\N{CJK UNIFIED IDEOGRAPH-89C4}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 21010 (\N{CJK UNIFIED IDEOGRAPH-5212}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 20010 (\N{CJK UNIFIED IDEOGRAPH-4E2A}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 21306 (\N{CJK UNIFIED IDEOGRAPH-533A}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 22495 (\N{CJK UNIFIED IDEOGRAPH-57DF}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 31751 (\N{CJK UNIFIED IDEOGRAPH-7C07}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 26631 (\N{CJK UNIFIED IDEOGRAPH-6807}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 325 plt.tight_layout() UserWarning: Glyph 31614 (\N{CJK UNIFIED IDEOGRAPH-7B7E}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 26354 (\N{CJK UNIFIED IDEOGRAPH-66F2}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 38754 (\N{CJK UNIFIED IDEOGRAPH-9762}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 25195 (\N{CJK UNIFIED IDEOGRAPH-626B}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 25551 (\N{CJK UNIFIED IDEOGRAPH-63CF}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 36335 (\N{CJK UNIFIED IDEOGRAPH-8DEF}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 24452 (\N{CJK UNIFIED IDEOGRAPH-5F84}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 35268 (\N{CJK UNIFIED IDEOGRAPH-89C4}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 21010 (\N{CJK UNIFIED IDEOGRAPH-5212}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 20010 (\N{CJK UNIFIED IDEOGRAPH-4E2A}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 21306 (\N{CJK UNIFIED IDEOGRAPH-533A}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 22495 (\N{CJK UNIFIED IDEOGRAPH-57DF}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 31751 (\N{CJK UNIFIED IDEOGRAPH-7C07}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 26631 (\N{CJK UNIFIED IDEOGRAPH-6807}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "D:/扫描机身项目8.15/python/1.py", line 328 plt.savefig('scan_path_visualization.png') UserWarning: Glyph 31614 (\N{CJK UNIFIED IDEOGRAPH-7B7E}) missing from font(s) DejaVu Sans. 可视化结果已保存为 'scan_path_visualization.png' Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 26354 (\N{CJK UNIFIED IDEOGRAPH-66F2}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 38754 (\N{CJK UNIFIED IDEOGRAPH-9762}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 25195 (\N{CJK UNIFIED IDEOGRAPH-626B}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 25551 (\N{CJK UNIFIED IDEOGRAPH-63CF}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 36335 (\N{CJK UNIFIED IDEOGRAPH-8DEF}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 24452 (\N{CJK UNIFIED IDEOGRAPH-5F84}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 35268 (\N{CJK UNIFIED IDEOGRAPH-89C4}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 21010 (\N{CJK UNIFIED IDEOGRAPH-5212}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 20010 (\N{CJK UNIFIED IDEOGRAPH-4E2A}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 21306 (\N{CJK UNIFIED IDEOGRAPH-533A}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 22495 (\N{CJK UNIFIED IDEOGRAPH-57DF}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 31751 (\N{CJK UNIFIED IDEOGRAPH-7C07}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 26631 (\N{CJK UNIFIED IDEOGRAPH-6807}) missing from font(s) DejaVu Sans. Warning (from warnings module): File "C:\Users\胡\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 861 func(*args) UserWarning: Glyph 31614 (\N{CJK UNIFIED IDEOGRAPH-7B7E}) missing from font(s) DejaVu Sans.
最新发布
12-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值