poj 2584 T-Shirt Gumbo

来源:互联网 发布:squid和nginx 编辑:程序博客网 时间:2024/05/17 22:14

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 1ST0 0 1 0 0ENDSTART 2SS TT0 0 1 0 0ENDSTART 4SM ML LX XT0 1 1 1 0ENDENDOFINPUT

Sample Output

T-shirts rock!I'd rather not wear a shirt anyway...I'd rather not wear a shirt anyway...

Key To Problem

题意:给定每个选手可以穿的衣服型号以及每种衣服型号有多少件,求是否可以使每位选手都能穿合适的衣服。
题解:可以将每种型号的每件衣服都单列为一种衣服,如果一位选手可以穿上这种衣服,那么久将该选手与这种衣服连边,之后用匈牙利算法求二分图即可。
//做完题之后去查题解看到有一种叫做二分图多重匹配的东西,之后发现这道题是裸题啊!果然本弱很弱做的题也很弱!

Code

#include <map>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 2005using namespace std;struct ss{    int next,to;};ss Edge[N*N];struct node{    int from,to;};node E[N];map<char,int>V;int n,tot,m;int head[N];char s[N];int f[N];int a[N];bool used[N];void init(){    tot=0;    memset(head,0,sizeof(head));    memset(a,0,sizeof(a));}void add(int x,int y){    Edge[++tot].next=head[x];    Edge[tot].to=y;    head[x]=tot;}bool dfs(int u){    for(int i=head[u];i;i=Edge[i].next)    {        int to=Edge[i].to;        if(!used[to])        {            used[to]=true;            if(f[to]==-1||dfs(f[to]))            {                f[to]=u;                return true;            }        }    }    return false;}int hungary(){    int cnt=0;    memset(f,-1,sizeof(f));    for(int i=1;i<=n;i++)    {        memset(used,0,sizeof(used));        if(dfs(i))cnt++;    }    return cnt;}int main(){    V['S']=1,V['M']=2,V['L']=3,V['X']=4,V['T']=5;    while(scanf("%s",s)&&strcmp(s,"ENDOFINPUT")!=0)    {        init();        scanf("%d",&n);        for(int i=1;i<=n;i++)        {            scanf("%s",s+1);            E[i].from=V[s[1]],E[i].to=V[s[2]];        }        for(int i=1;i<=5;i++)        {            int x;            scanf("%d",&x);            while(x)            {                m++;                for(int j=1;j<=n;j++)                    if(E[j].from<=i&&E[j].to>=i)add(j,m);                x--;            }        }        if(hungary()==n)puts("T-shirts rock!");        else puts("I'd rather not wear a shirt anyway...");        scanf("%s",s);        memset(s,0,sizeof(s));    }    return 0;}
0 0
原创粉丝点击