当前位置: 首页 > news >正文

POJ2584_T-Shirt 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 99999999
using 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: 
  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

South Central USA 2003


相关文章:

  • 以精益的眼光重新关注电子商务
  • leetcode-000-序
  • cropper使用在线图片的问题
  • 在SAE搭建Python+Django+MySQL(基于Windows)
  • Java 单例模式
  • TP5分页类
  • 新CSS伪类:focus-within
  • 如果一个人
  • xmemcached 0.60 优化过程
  • 生产环境硬件使用总结
  • xmemcached发布1.1.2 (权重、noreply、spring集成)
  • tomcat8.5报错
  • Clojure世界:利用HouseMD诊断clojure
  • pat解题报告【1082】
  • Java IO详解(七)------随机访问文件流
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • 【5+】跨webview多页面 触发事件(二)
  • 【知识碎片】第三方登录弹窗效果
  • 0x05 Python数据分析,Anaconda八斩刀
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • Codepen 每日精选(2018-3-25)
  • Create React App 使用
  • DOM的那些事
  • Python中eval与exec的使用及区别
  • React-flux杂记
  • React-生命周期杂记
  • Redis在Web项目中的应用与实践
  • Solarized Scheme
  • Vue ES6 Jade Scss Webpack Gulp
  • Vue.js 移动端适配之 vw 解决方案
  • Vue--数据传输
  • 半理解系列--Promise的进化史
  • 坑!为什么View.startAnimation不起作用?
  • 你真的知道 == 和 equals 的区别吗?
  • 使用 QuickBI 搭建酷炫可视化分析
  • 7行Python代码的人脸识别
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • $.extend({},旧的,新的);合并对象,后面的覆盖前面的
  • (1)(1.13) SiK无线电高级配置(六)
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (二)JAVA使用POI操作excel
  • (分布式缓存)Redis分片集群
  • (剑指Offer)面试题41:和为s的连续正数序列
  • (小白学Java)Java简介和基本配置
  • .NET MAUI学习笔记——2.构建第一个程序_初级篇
  • .NET/C# 使用反射注册事件
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
  • .php文件都打不开,打不开php文件怎么办
  • /usr/local/nginx/logs/nginx.pid failed (2: No such file or directory)
  • @Transactional注解下,循环取序列的值,但得到的值都相同的问题
  • [1]-基于图搜索的路径规划基础
  • [2016.7 Day.4] T1 游戏 [正解:二分图 偏解:奇葩贪心+模拟?(不知如何称呼不过居然比std还快)]
  • [Android]使用Android打包Unity工程