Problem Description
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
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.
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 个星球居住,然后给出n行吗列,表示第i人是否愿意到第j星球,1表示愿意,0表示不愿意,最后一行给出m个星球适合居住的人数,选择情况和星球居住上限有限制,问是否能全部满足要求。
思路:
匈牙利算法可以解决多重匹配,原理和二分图最大匹配很像。注意不要把可以匹配多个的点分割然后按照正常的二分匹配来做,那样肯定会挂的。
解决多重匹配就是记录一下多重匹配的点(简称Y方点)已经匹配了Pi个点。如果Pi<Ki那么就直接上了,否则的话继续搜索Yi已经匹配的每一个点并将Yi染色。
因为Yi搜一次就需要染色了,而且Y方点最多是10个,所以每次找增广路的深度最多是10,这样就很快了。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=100005;
int map[N][10],link[10][N],vlink[10];
int vis[10],w[10];
int n,m;
int find(int u)
{
for(int v=0;v<m;v++)
{
if(map[u][v]&&!vis[v])
{
vis[v]=1;
if(vlink[v]<w[v])
{
link[v][vlink[v]++]=u;
return 1;
}
for(int i=0;i<vlink[v];i++)
{
if(find(link[v][i]))
{
link[v][i]=u;
return 1;
}
}
}
}
return 0;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
scanf("%d",&map[i][j]);
for(int i=0;i<m;i++)
scanf("%d",&w[i]);
memset(vlink,0,sizeof(vlink));
int flag=0;
for(int i=0;i<n;i++)
{
memset(vis,0,sizeof(vis));
int p=find(i);
if(!p)
{
flag=1;
break;
}
}
if(flag)
printf("NO\n");
else
printf("YES\n");
}
}