POJ 2584

T-Shirt Gumbo
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3161 Accepted: 1478

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:

Start line - A single line:
START X

where (1 <= X <= 20) is the number of contestants demanding shirts.
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.
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.
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...

Source

South Central USA 2003


题意:给定需要寸衫的人的数量,下面是每个人能穿的寸衫范围,然后是每件寸衫型号对应的数量,输出如果人都用满足,则输出。。。否则输出。。。


二分多重匹配,这里人为左边集合,全部衣服为右边集合,注意不是衣服种类,是每个衣服种类对应数量累加之和,所以在匹配连通的时候,a[i]为1到i种类型为止的所有数量,匹配连通时,左边T[i].l-1,到起始匹配种类的左边一个种类,a[T[i].l-1]就是起始匹配种类左边的一个种类之前的全部衣服和,再加上1就是起始匹配种类的第一件衣服了的标号了(从1到a[5]),这里右边集合跟上次做的那个上课的二分匹配一样,衣服数和课时数作为标号了。好了做了这一个题,对二分匹配更了解了。

eg:


型号:S M L X T

对应:0 1 2 2 1

a[i]值:0 1 3 5 6

当范围为XT时,表明这个人XT号的寸衫都能穿,则和4,5,6(X的两件,T的一件)都连通,可以用for(j=a[T[i].l-1]+1;j<=a[T[i].r];++j)    ee[i][j]=1 处理了吧。

#include<iostream>
#include<vector>
#include<cstdio>
#include<map>
#include<cstring>
using namespace std;
bool vis[205],ee[30][205];
int a[6],match[205];
struct TT{
    int l,r;
}T[30];

int dfs(int u){
    for(int i=1;i<=a[5];++i){ //这里右边的集合是衣服总数量,也就有如果一种寸衫有多种就可以满足多个人的同一种寸衫的需要
        if(ee[u][i]==1&&vis[i]==0){
            vis[i]=1;
            if(!match[i]||dfs(match[i])){
                match[i]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    char s[10];
    int n,i,j;
    map<char,int>mp;
    mp['S']=1,mp['M']=2,mp['L']=3,mp['X']=4,mp['T']=5;
    while(~scanf("%s",s)){
        if(strcmp(s,"ENDOFINPUT")==0)
            break;
        scanf("%d",&n);
        memset(ee,0,sizeof(ee));
        memset(match,0,sizeof(match)); //初始化

        for(i=1;i<=n;++i){
            scanf("%s",s);
            T[i].l=mp[s[0]],T[i].r=mp[s[1]];
        }
        a[0]=0;
        for(i=1;i<=5;++i) {scanf("%d",&a[i]); a[i]+=a[i-1];}//右边集合数量用a[i]处理。
        for(i=1;i<=n;++i)
            for(j=a[T[i].l-1]+1;j<=a[T[i].r];++j)
                ee[i][j]=1;//匹配右边的集合,因为同种型号有多的,所以先T[i].l-1到前一种型号后加上1得到本型号的第一件寸衫
        int ans=0;

        for(i=1;i<=n;++i){
            memset(vis,0,sizeof(vis));
            ans+=dfs(i);
        }
        scanf("%s",s);
        if(ans>=n) printf("T-shirts rock!");
        else
            printf("I'd rather not wear a shirt anyway...");
        printf("
");
    }
    return 0;
}

            

原文地址:https://www.cnblogs.com/mingrigongchang/p/6246210.html