poj 2584 T-Shirt Gumbo - 二分匹配

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...

Source

(转自http://poj.org/problem?id=2584)

  首先讲一下题目的大意。一些人,每人能接受一些T恤的尺寸(从一个尺寸到另外一个尺寸)
然后有各种T恤,问,能不能匹配,如果能,输出“T-shirts rock!”, 否则写出“I'd rather not
wear a shirt anyway... ”
  这道题可以用二分匹配(虽然dfs都可以水过)
  每一件T恤用一个数组表示数量,如果当前数量未满,就增加一个使用它的人,否则dfs每一个
判断能否选
Code
  1 /*
  2  * poj.org
  3  * Problem#2584
  4  * Accepted
  5  * Time:0ms
  6  * Memory:176k
  7  */
  8 #include<iostream>
  9 #include<cstdio>
 10 #include<fstream>
 11 #include<cstring>
 12 #include<queue>
 13 #include<cctype>
 14 #include<algorithm>
 15 #include<vector>
 16 #define INCLUDER
 17 using namespace std;
 18 typedef bool boolean;
 19 #define INF 0xfffffff
 20 #define smin(a, b) a = min(a, b)
 21 #define smax(a, b) a = max(a, b)
 22 template<typename T>
 23 inline void readInteger(T& u){
 24     char x;
 25     while(!isdigit((x = getchar())));
 26     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
 27     ungetc(x, stdin);
 28 }
 29 
 30 int n;
 31 int *range_low, *range_high;
 32 vector<int> match[6];
 33 boolean used[6];
 34 int had[6];
 35 
 36 boolean find(int node){
 37     for(int i = range_low[node]; i <= range_high[node]; i++){
 38         if(!used[i])    continue;
 39         used[i] = false;
 40         if((signed)match[i].size() < had[i]){
 41             match[i].push_back(node);
 42             return true;
 43         }
 44         for(int j = 0; j < (signed)match[i].size(); j++){
 45             if(find(match[i][j])){
 46                 match[i][j] = node;
 47                 return true;
 48             }
 49         }
 50     }
 51     return false;
 52 }
 53 
 54 int key[50];
 55 char buf[15];
 56 
 57 inline void qj_init(){
 58     key['S' - 'A'] = 1;
 59     key['M' - 'A'] = 2;
 60     key['L' - 'A'] = 3;
 61     key['X' - 'A'] = 4;
 62     key['T' - 'A'] = 5;
 63 }
 64 int counta = 0;
 65 inline boolean init(){
 66     string s;
 67     scanf("%s", buf);
 68     if(strcmp(buf, "START") != 0)    return false;
 69     readInteger(n);
 70     counta = 0;
 71     range_low = new int[(const int)(n + 1)];
 72     range_high = new int[(const int)(n + 1)];
 73     getchar();
 74     for(int i = 1; i <= n; i++){
 75         char l = getchar();
 76         char h = getchar();
 77         range_low[i] = key[l - 'A'];
 78         range_high[i] = key[h - 'A']; 
 79         getchar();
 80     }
 81     for(int i = 1; i <= 5; i++){
 82         readInteger(had[i]);
 83         counta += had[i];
 84     }
 85     scanf("%s", buf);
 86     return true;
 87 }
 88 
 89 inline boolean solve(){
 90     for(int i = 1; i <= n; i++){
 91         memset(used, true, sizeof(used));
 92         if(!find(i))    return false;
 93     }
 94     return true;
 95 }
 96 
 97 inline void clean(){
 98     delete[] range_low;
 99     delete[] range_high;
100     for(int i = 1; i <= 5; i++){
101         match[i].clear();
102     }
103 }
104 
105 ///Main Funtion
106 int main(int argc, char* argv[]){
107     qj_init();
108     while(init()){
109         if(counta >= n && solve()){
110             printf("T-shirts rock!");
111         }else{
112             printf("I'd rather not wear a shirt anyway...");
113         }
114         putchar('\n');
115         clean();
116     }
117     return 0;
118 }
posted @ 2016-10-11 21:04  阿波罗2003  阅读(190)  评论(0编辑  收藏  举报