hdu 3605 Escape/poj 2584 T-Shirt Gumbo(二分图多重匹配)

Escape

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


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
题意:地球上有n个人,有m颗星球,这n个人分别想去某些星球,但星球有人数限制

现在问所有人是否可以都到自己想去的某个星球

思路:多重匹配模板题。当然也可以网络流最大流,这里用匈牙利算法

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100050
#define M 15
int vis[M];///line当前与y结点相连的x的编号,vis防止成环,记录y结点访问情况
int ma[N][M],line[N][M];
int n,m,num[M],cnt[M];///num记录每个Y点可以有多少个,cnt记录当前y点匹配个数
int can(int t)
{
    for(int i=1;i<=m;i++)
    {
        if(!vis[i]&&ma[t][i])
        {
            vis[i]=1;
            if(cnt[i]<num[i])
            {
                line[i][cnt[i]]=t;
                cnt[i]++;
                return 1;
            }
            else
            {
                for(int j=0;j<num[i];j++)
                {
                    if(can(line[i][j]))
                    {
                        line[i][j]=t;
                        return 1;
                    }
                }
            }
        }
    }
    return 0;
}
int main()
{
    int s,d,t;
    while(~scanf("%d %d",&n,&m))
    {
        memset(cnt,0,sizeof(cnt));
        memset(ma,0,sizeof(ma));
        for(int i=1;i<=n;i++)
        {
           for(int j=1;j<=m;j++)
           {
               scanf("%d",&s);
               if(s) ma[i][j]=1;
           }
        }
        for(int i=1;i<=m;i++)
            scanf("%d",&num[i]);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(can(i)) ans++;
            else break;
        }
        if(ans==n) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

T-Shirt Gumbo
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3103 Accepted: 1465

Description

Boudreaux and Thibodeaux are student volunteers for this year's ACM South Central Region's programming contest. One of their duties is to distribute the contest T-shirts to arriving teams. The T-shirts had to be ordered in advance using an educated guess as to how many shirts of each size should be needed. Now it falls to Boudreaux and Thibodeaux to determine if they can hand out T-shirts to all the contestants in a way that makes everyone happy.

Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. 

A single data set has 4 components: 
  1. Start line - A single line: 
    START X 

    where (1 <= X <= 20) is the number of contestants demanding shirts. 
  2. Tolerance line - A single line containing X space-separated pairs of letters indicating the size tolerances of each contestant. Valid size letters are S - small, M - medium, L - large, X - extra large, T - extra extra large. Each letter pair will indicate the range of sizes that will satisfy a particular contestant. The pair will begin with the smallest size the contestant will accept and end with the largest. For example: 
    MX 

    would indicate a contestant that would accept a medium, large, or extra large T-shirt. If a contestant is very picky, both letters in the pair may be the same. 
  3. Inventory line - A single line: 
    S M L X T 

    indicating the number of each size shirt in Boudreaux and Thibodeaux's inventory. These values will be between 0 and 20 inclusive. 
  4. End line - A single line: 
    END 

After the last data set, there will be a single line: 
ENDOFINPUT 

Output

For each data set, there will be exactly one line of output. This line will reflect the attitude of the contestants after the T-shirts are distributed. If all the contestants were satisfied, output: 

T-shirts rock! 

Otherwise, output: 
I'd rather not wear a shirt anyway... 

Sample Input

START 1
ST
0 0 1 0 0
END
START 2
SS TT
0 0 1 0 0
END
START 4
SM ML LX XT
0 1 1 1 0
END
ENDOFINPUT

Sample Output

T-shirts rock!
I'd rather not wear a shirt anyway...
I'd rather not wear a shirt anyway...
题意:有n件个人,每个人可以穿一定区间大小的衣服,比如[M,T],则此人可以穿M,L,X,T大小的衣服,有5款不同大小的衣服,各有一定数量件

现问你所有人是否都能穿上合身的衣服

思路:又是一个多重匹配模板题,在最大匹配的基础上把是否匹配过改成匹配次数即可

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100
#define M 6
int vis[M];///line当前与y结点相连的x的编号,vis防止成环,记录y结点访问情况
int ma[N][M],line[N][M];
int n,m,num[M],cnt[M];///num记录每个Y点可以有多少个,cnt记录当前y点匹配个数
int can(int t)
{
    for(int i=1;i<=m;i++)
    {
        if(!vis[i]&&ma[t][i])
        {
            vis[i]=1;
            if(cnt[i]<num[i])
            {
                line[i][cnt[i]]=t;
                cnt[i]++;
                return 1;
            }
            else
            {
                for(int j=0;j<num[i];j++)
                {
                    if(can(line[i][j]))
                    {
                        line[i][j]=t;
                        return 1;
                    }
                }
            }
        }
    }
    return 0;
}
int f(char c)
{
    if(c=='S') return 1;
    else if(c=='M') return 2;
    else if(c=='L') return 3;
    else if(c=='X') return 4;
    else if(c=='T') return 5;
}
void get(int id,char *b)
{
    int l=f(b[0]),r=f(b[1]);
    for(int i=l;i<=r;i++)
        ma[id][i]=1;
}
int main()
{
    int s,d,t;
    m=5;
    char a[15],b[2];
    while(1)
    {
        memset(cnt,0,sizeof(cnt));
        memset(ma,0,sizeof(ma));
        scanf("%s",a);
        if(strcmp(a,"ENDOFINPUT")==0) break;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",b);
            get(i,b);
        }
        for(int i=1;i<=m;i++)
            scanf("%d",&num[i]);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(can(i)) ans++;
            else break;
        }
        scanf("%s",a);
        if(ans==n) printf("T-shirts rock!\n");
        else printf("I'd rather not wear a shirt anyway...\n");
    }
    return 0;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值