hdu 3065 二分图多重匹配

来源:互联网 发布:什么是键值数据库 编辑:程序博客网 时间:2024/06/05 03:28
题目链接点击打开链接

Escape

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 11261    Accepted Submission(s): 2709


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
 

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

Sample Input
1 1112 21 01 01 1
 

Sample Output
YESNO
 

Source
2010 ACM-ICPC Multi-University Training Contest(17)——Host by ZSTU
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  2883 2732 3338 2485 3607 


题目大意:有n个人要移民到m个星球,其中每个星球可以有多个移民但是。

这是典型的多重匹配问题

我们依旧用匈牙利算法来解决,只不过匈牙利算法中是开了一个一维数组来保存与y对应的x,这里会有多个x与y对应,因此我们就需要开一个二维数组来保存,数组的第一个下标表示y的序号,第二个下标表示与y对应的是第几个x的序号。我们在给X配对时,如果所对应的Y没有达到限制,就直接让两者配对,否则我们就让与Y对应的其他几个X让位置,并且注意要记录与Y匹配的X的数量是几个。

AC代码:

#include<iostream>#include<string.h>#include<stdio.h>using namespace std;int n,m;int map[100010][11];int together[11][100010];bool check[11];bool flag;int a[11];int cnt[11];bool dfs(int x){    for(int j=1;j<=m;j++)    {        if(map[x][j]&&!check[j])        {            check[j]=true;            if(cnt[j]<a[j])            {                together[j][++cnt[j]]=x;                return true;            }            for(int k=1;k<=cnt[j];k++)            {                if(dfs(together[j][k]))                   {                       together[j][k]=x;                       return true;                   }            }        }    }    return false;}int main(){    while(~scanf("%d%d",&n,&m))     {        flag=false;        memset(cnt,0,sizeof(cnt));        for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)        scanf("%d",&map[i][j]);        for(int i=1;i<=m;i++)            scanf("%d",&a[i]);        for(int i=1;i<=n;i++)        {            memset(check,false,sizeof(check));            if(!dfs(i))            {                flag=true;                break;            }        }        if(flag)            printf("NO\n");        else            printf("YES\n");     }     return 0;}







原创粉丝点击