pku2584

来源:互联网 发布:淘宝至宝比官网便宜 编辑:程序博客网 时间:2024/06/01 19:12

其实就是模版题,最大流

建一个超级源点和一个超级汇点,超级源点和每个选手连一条权值为1的边,每个选手和他适合的衣服连一条权值的为1的,

每种size的衣服和超级汇点连一条以这种衣服的数量为权值的边。然后求最大流就行了。wa了两天啊,郁闷。一开始建图

出错,后来是最大流写错了。

/*
 * File:   pku2584.cpp
 * Author: chenjiang
 *
 * Created on 2010年4月15日, 下午8:06
 */

#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <stack>
#include <map>
#include <queue>
using namespace std;
#define Max 30
#define inf 9999999
int pre[Max], flow[Max][Max], mapmap[Max][Max], min_flow[Max];
char ch[Max];
int x;
int source, sink;

map<char, int>my;
int a[6];

int min(int a, int b) {
    if (a <= b)return a;
    else return b;
}

/*
int max_flow()
{
    int d[Max],p,q,t,i,j;
    if(source==sink)return inf;
    for(i=0;i<sink;i++)
    {
        for(j=0;j<sink;j++)
        {
            flow[i][j]=0;
        }
    }

    while(1)
    {
        for(i=0;i<=sink;pre[i++]=0);
        pre[t=source]=source+1
    }
}
 * */
int max_flow() {
    int i, j, k;
    memset(flow, 0, sizeof (flow));
    int ans = 0;
    while (1) {
        queue<int> Q;
        Q.push(source);
        memset(pre, -1, sizeof (pre));
        min_flow[source] = inf;
        pre[source] = -2;
        while (!Q.empty()) {
            int temp = Q.front();
            Q.pop();
            for (i = source; i <= sink; i++) {
                if (pre[i] == -1 && flow[temp][i] < mapmap[temp][i]) {
                    Q.push(i);
                    pre[i] = temp;
                    min_flow[i] = min(min_flow[temp], (mapmap[temp][i] - flow[temp][i]));
                }
            }
            if (pre[sink] != -1) {
                k = sink;
                while (pre[k] >= source) {
                    flow[pre[k]][k] += min_flow[sink];
                    flow[k][pre[k]] = -flow[pre[k]][k];
                    k = pre[k];
                }
                break;
            }
        }
        if (pre[sink] == -1)return ans;
        else ans += min_flow[sink];
    }
}

/*
 *
 */
int main(int argc, char** argv) {

    int i, j;
    string str;
    my['S'] = 1;
    my['M'] = 2;
    my['L'] = 3;
    my['X'] = 4;
    my['T'] = 5;
    //freopen("a.in", "r", stdin);
    while (cin >> str) {
        memset(mapmap, 0, sizeof (mapmap));
        if (str == "ENDOFINPUT")break;
        scanf("%d", &x);
        source = 0;
        sink = x + 5 + 1;
        for (i = 1; i <= x; i++) {
            cin >> ch;
            for (j = my[ch[0]]; j <= my[ch[1]]; j++) {
                mapmap[i][j + x] = 1;
                //printf("mapmap[%d][%d]=%d/n",i,j+x,mapmap[i][j+x]);
            }
        }
        for (i = 1; i <= 5; i++) {
            scanf("%d", &a[i]);
            mapmap[i + x][sink] = a[i];
            //printf("mapmap[%d][%d]=%d/n",i+x,sink,mapmap[i+x][sink]);
        }
        cin >> ch;
        for (i = 1; i <= x; i++) {
            mapmap[source][i] = 1;
            //printf("mapmap[%d][%d]=%d/n",source,i,mapmap[source][i]);
            //cout<<endl;
        }
        /*
                for(i=source;i<=sink;i++)
                {
                    for(j=source;j<=sink;j++)
                    {
                        if(mapmap[i][j])printf("mapmap[%d][%d]=%d/n",i,j,mapmap[i][j]);
                    }
                }
         */
        int ans = max_flow();
        if (ans == x)printf("T-shirts rock!/n");
        else printf("I'd rather not wear a shirt anyway.../n");
    }
    return (EXIT_SUCCESS);
}