hdu 3065 二分图多重匹配

题目链接点击打开链接

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 1 1 1 2 2 1 0 1 0 1 1
 

Sample Output
   
   
YES NO
 

Source
 

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







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值