POJ2584_T-Shirt Gumbo(二分圖多重最大匹配/最大流),gumbo

來源:互聯網
上載者:User

POJ2584_T-Shirt Gumbo(二分圖多重最大匹配/最大流),gumbo

解題報告

http://blog.csdn.net/juncoder/article/details/38239367

題目傳送門

題意:

X個參賽選手,每個選手有衣服大小的範圍,5種大小的隊服,求是否能使每個選手都拿到符合自己大小範圍的衣服。

思路:

X人5種衣服,有的人選的衣服可能大小一樣,這樣就是二分圖的多重最大匹配。源點到5種衣服的容量就是衣服的數量。

#include <queue>#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define inf 99999999using namespace std;int n,mmap[30][30],m,l[30],t;int T_shirt[30];int bfs(){    queue<int >Q;    Q.push(0);    memset(l,-1,sizeof(l));    l[0]=0;    while(!Q.empty()) {        int u=Q.front();        Q.pop();        for(int i=0; i<=m; i++) {            if(l[i]==-1&&mmap[u][i]) {                l[i]=l[u]+1;                Q.push(i);            }        }    }    if(l[m]>1)return 1;    else return 0;}int dfs(int x,int f){    int a;    if(x==m)return f;    for(int i=0; i<=m; i++) {        if(mmap[x][i]&&l[i]==l[x]+1&&(a=dfs(i,min(f,mmap[x][i])))) {            mmap[x][i]-=a;            mmap[i][x]+=a;            return a;        }    }    l[x]=-1;    return 0;}int main(){    int i,j;    char str[100];    char s[10];    T_shirt['S'-'A']=1;    T_shirt['M'-'A']=2;    T_shirt['L'-'A']=3;    T_shirt['X'-'A']=4;    T_shirt['T'-'A']=5;    while(cin>>str) {        memset(mmap,0,sizeof(mmap));        if(!strcmp(str,"ENDOFINPUT"))            break;        if(!strcmp(str,"END"))            continue;        cin>>n;        m=n+5+1;        for(i=1; i<=n; i++) {            cin>>s;            int a=T_shirt[s[0]-'A'];            int b=T_shirt[s[1]-'A'];            if(a>b)                swap(a,b);            for(j=a; j<=b; j++) {                mmap[j][i+5]=1;            }            mmap[i+5][m]=1;        }        for(i=1; i<=5; i++) {            scanf("%d",&t);            mmap[0][i]=t;        }        int ans=0;        cin>>str;        while(bfs())            while(t=dfs(0,inf))                ans+=t;        if(ans==n)            cout<<"T-shirts rock!"<<endl;        else cout<<"I'd rather not wear a shirt anyway..."<<endl;        if(!strcmp(str,"END"))continue;    }    return 0;}

T-Shirt Gumbo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2621   Accepted: 1223

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: 

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

Source

South Central USA 2003





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.