hdu 3605 Escape (二分图多重匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605

2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.

Input

More set of test data, the beginning of each data is n (1 <= n <= 100000), m (1 <= m <= 10) n indicate there n people on the earth, m representatives m planet, planet and people labels are from 0. Here are n lines, each line represents a suitable living conditions of people, each row has m digits, the ith digits is 1, said that a person is fit to live in the ith-planet, or is 0 for this person is not suitable for living in the ith planet.
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000

Output

Determine whether all people can live up to these stars
If you can output YES, otherwise output NO.

Sample Input

 

1 1 1 1 2 2 1 0 1 0 1 1

Sample Output

 

YES NO

n个人移民到m个星球上,每个星球限制了人数,用一个0-1矩阵表示第i行的人是否愿意移民到第j列个星球上。最后输出所有人是否能顺利移民。

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int adj[maxn][12],mat[12][maxn];
int w[15],cnt[15],f[15];
int n, m;
int dfs(int x)
{
	for (int i = 0; i < m; i++)
	{
		if (!f[i] && adj[x][i])
		{
			f[i] = 1;
			if (cnt[i] < w[i])
			{
				mat[i][cnt[i]++] = x;
				return true;
			}
			for (int j = 0; j < cnt[i]; j++)
			{
				if (dfs(mat[i][j]))
				{
					mat[i][j] = x;
					return true;
				}
			}
		}
	}
	return false;
}
bool ok()
{
	memset(cnt, 0, sizeof(cnt));
	for (int i = 0; i < n; i++)
	{
		memset(f, 0, sizeof(f));
		if (!dfs(i))
		{
			return false;
		}
	}
	return true;
}
int main()
{
	//freopen("C:/input.txt", "r", stdin);
	while (~scanf("%d%d", &n, &m))
	{
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				scanf("%d", &adj[i][j]);
			}
		}
		for (int i = 0; i < m; i++)
		{
			scanf("%d", &w[i]);
		}
		puts(ok() ? "YES" : "NO");
	}
	return 0;
}

	

猜你喜欢

转载自blog.csdn.net/Evildoer_llc/article/details/83021211
今日推荐