解题报告

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

POJ2584_T-Shirt Gumbo(二分图多重最大匹配/最大流)的更多相关文章

  1. POJ 2584 T-Shirt Gumbo (二分图多重最大匹配)

    题意 现在要将5种型号的衣服分发给n个参赛者,然后给出每个参赛者所需要的衣服的尺码的大小范围,在该尺码范围内的衣服该选手可以接受,再给出这5种型号衣服各自的数量,问是否存在一种分配方案使得每个选手都能 ...

  2. 二分图的最大匹配——最大流EK算法

    序: 既然是个图,并且求边数的最大值.那么这就可以转化为网络流的求最大流问题. 只需要将源点与其中一子集的所有节点相连,汇点与另一子集的所有节点相连,将所有弧的流量限制置为1,那么最大流 == 最大匹 ...

  3. hdu3605 Escape 二分图多重匹配/最大流

    2012 If this is the end of the world how to do? I do not know how. But now scientists have found tha ...

  4. POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65 ...

  5. POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

  6. POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 6 ...

  7. <转> 二分图多重匹配问题

    在二分图最大匹配中,每个点(不管是X方点还是Y方点)最多只能和一条匹配边相关联,然而,我们经常遇到这种问题,即二分图匹配中一个点可以和多条匹配边相关联,但有上限,或者说,Li表示点i最多可以和多少条匹 ...

  8. 网络流24题 第五题 - PowerOJ1740 CodeVS1905 圆桌问题 二分图多重匹配 网络最大流

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - PowerOJ1740 - 有SPJ - 推荐 题目传送门 - CodeVS1905 - 无SPJ - 0% ...

  9. 【网络流24题】No.7 试题库问题 (最大流,二分图多重匹配)

    [题意] 假设一个试题库中有 n 道试题. 每道试题都标明了所属类别. 同一道题可能有多个类别属性.现要从题库中抽取 m 道题组成试卷.并要求试卷包含指定类型的试题. 试设计一个满足要求的组卷算法. ...

随机推荐

  1. Entity Framework 4.3 中使用存储过程

    尽管 Entity Framework 4.3 都已经发布了,且表示在 EF 5 中性能将会有很大提升.但很多存储过程控,始终不会放弃使用存储过程,那今天就让我们看看在 EF 4.3 中怎么使用存储过 ...

  2. ansible基础知识

    安装ansible epel源 第一步: 下载epel源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel- ...

  3. C++ friend关键字

    友元:友元函数 友元类. 友元函数:不属于任何类,只是在类中声明一下(可以放在 private 或者 public,没有区别),告诉这个类,这个函数是你的朋友,当然朋友不是白当的:这个函数可以访问你的 ...

  4. 321 Create Maximum Number 拼接最大数

    已知长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,直观地表示两个自然数各位上的数字.现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中 ...

  5. 11 在C#中写文件

    在这个练习中,我们来学习如何把我们想要的东西写到文件中.我们在这个练习中还是使用File类中的方法来完成写文件的操作. 在这个练习中我们要用C#创建一个纯文本文件ex11.txt 放到c盘的Exerc ...

  6. Spring boot中的定时任务(计划任务)

    从Spring3.1开始,计划任务在Spring中实现变得异常的简单.首先通过配置类注解@EnableScheduling来开启对计划任务的支持,然后再要执行的计划任务的方法上注释@Scheduled ...

  7. ios数据的基本类型和流程控制

    swift的声明变量方式和js是类似的.基本类型基本都和java的差不多,多了字符类型. let:用于声明常量: var:用于声明变量: 基本类型有:double,float,Int(数字类型):bo ...

  8. mysql索引初认识

    mysql> use mysql; Database changed mysql> show index from user; +-------+------------+-------- ...

  9. JS——缓慢动画封装案例

    手风琴 1.排他思想 2.ul宽度需要大一点,防止li撑开跑下去 3.一个变大其他所有变小,变小不能太小,不然会出现空白 <!DOCTYPE html> <html lang=&qu ...

  10. ASP.net参数传递总结

    同一页面.aspx与.aspx.cs之间参数传递 1. .aspx.cs接收.aspx的参数:由于.aspx和.aspx.cs为继承关系,所以.aspx.cs可以直接对.aspx中的ID进行值提取,具 ...