HDU3605 Escape (二分图多重匹配 邻接矩阵or链式向前星)

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


Escape

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4464    Accepted Submission(s): 1176


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

//http://acm.hdu.edu.cn/showproblem.php?pid=3605

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100005

int n,m;

struct Edge
{
    int to  ,next;
}edge[N*12];
int headList[N];

int limit[12];
int vis[12];
int nowLive[12];
int live[12][N];
//int mp[N][12];
int top;
int in()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' || ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}
/*
//邻接矩阵实现多重分配
int dfs(int k)
{
    int i;
    for(i=0; i<m; i++)
    {
        if(!vis[i] && mp[k][i]) //判断该人是否适合住该行星,和该行星是否被浏览过
        {
            vis[i]=1;  //标记该行星
            if(nowLive[i] < limit[i])  //该行星已居住人数是否超过限制
            {
                live[i][nowLive[i]++] = k;  //没有则住进该行星,当前行星已住人数+1;返回1;
                return 1;
            }

            int j;
            for(j=0; j<nowLive[i]; j++)  //如果当前行星已住人数超过限制
            {
                if(dfs(live[i][j])) //则将已住在该行星上的人一个个搬到其他行星上去,这里就是找增广路的过程
                {
                    live[i][j]=k;  //该人住进该行星,可以找到其他行星住的人搬走
                    return 1;
                }
            }
        }
    }
    return 0;
}
*/

//链式向前星实现多重分配
void add(int a,int b)
{
    edge[top].to=b;edge[top].next=headList[a];headList[a]=top++;
}

int dfs(int k)
{
    int x;
    for(x = headList[k] ; x!=-1 ; x=edge[x].next)
    {
        int y = edge[x].to;

        if(!vis[y])
        {
            vis[y]=1;

            if(nowLive[y]<limit[y])
            {
                live[y][nowLive[y]++]=k;
                return 1;
            }
            int j;
            for(j=0;j<nowLive[y];j++)
            {
                if(dfs(live[y][j]))
                {
                    live[y][j]=k;
                    return 1;
                }
            }
        }
    }
    return 0;
}
int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        top=0;
        int i,j,a;
        memset(nowLive,0,sizeof(nowLive));
        memset(live,0,sizeof(live));
        memset(headList,-1,sizeof(headList));
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
               // mp[i][j]=in();
               //scanf("%d",&a);
               a=in();
               if(a)
                add(i,j);
            }
        }
        for(i=0; i<m; i++)
            scanf("%d",&limit[i]);
        int flag=1;
        for(i=0; i<n; i++)
        {
            memset(vis,0,sizeof(vis));
            if(!dfs(i))
            {
                flag=0;
                break;
            }
        }
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值