POJ 2584 最大匹配。匈牙利算法
2011-01-16 00:48
357 查看
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:
Start line - A single line:
START X
where (1 <= X <= 20) is the number of contestants demanding shirts.
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.
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.
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
Sample Output
Source
South Central USA 2003
题目不难,关键在于建图
Source Code
Source Code
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1437 | Accepted: 635 |
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:
Start line - A single line:
START X
where (1 <= X <= 20) is the number of contestants demanding shirts.
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.
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.
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
题目不难,关键在于建图
Source Code
Problem: 2584 | User: bingshen | |
Memory: 144K | Time: 0MS | |
Language: C++ | Result: Accepted |
#include<stdio.h> #include<algorithm> #include<string.h> using namespace std; bool map[105][105]; bool used[105]; int link[105]; int a[10]; char size[105][2]; int sz[105][2]; int n,m; void init() { memset(map,0,sizeof(map)); memset(link,-1,sizeof(link)); m=0; } bool dfs(int v) { int i; for(i=0;i<n;i++) { if(!used[i]&&map[v][i]) { used[i]=true; if(link[i]==-1||dfs(link[i])) { link[i]=v; return true; } } } return false; } int main() { int i,j,k; char control[20],str[10]; while(scanf("%s",control)!=EOF) { if(strcmp(control,"ENDOFINPUT")==0) break; init(); if(strcmp(control,"START")==0) { scanf("%d",&n); for(i=0;i<n;i++) { scanf("%s",size[i]); if(size[i][0]=='S') sz[i][0]=1; if(size[i][0]=='M') sz[i][0]=2; if(size[i][0]=='L') sz[i][0]=3; if(size[i][0]=='X') sz[i][0]=4; if(size[i][0]=='T') sz[i][0]=5; if(size[i][1]=='S') sz[i][1]=1; if(size[i][1]=='M') sz[i][1]=2; if(size[i][1]=='L') sz[i][1]=3; if(size[i][1]=='X') sz[i][1]=4; if(size[i][1]=='T') sz[i][1]=5; } for(i=1;i<=5;i++) scanf("%d",&a[i]); for(i=1;i<=5;i++) { for(j=0;j<a[i];j++) { for(k=0;k<n;k++) { if(sz[k][0]<=i&&i<=sz[k][1]) { map[m][k]=true; // printf("(%d,%d)/n",m,k); } } m++; } } int ans=0; for(i=0;i<m;i++) { memset(used,0,sizeof(used)); if(dfs(i)) ans++; } if(ans==n) printf("T-shirts rock!/n"); else printf("I'd rather not wear a shirt anyway.../n"); scanf("%s",str); } } return 0; }
相关文章推荐
- poj 2446 二分图最大匹配 匈牙利算法
- poj 3894 System Engineer (二分图最大匹配--匈牙利算法)
- poj 1325 Machine Schedule(最小点覆盖即最大匹配,匈牙利算法)
- POJ1469(匈牙利算法求二分图最大匹配)
- (模板题)poj 3041 Asteroids(二分图的最大匹配匈牙利算法)
- POJ 1274The Perfect Stall (二分图最大匹配问题,匈牙利算法实现)
- poj 1274The Perfect Stall(二分最大匹配法——匈牙利算法)
- 【二分图最大匹配】【匈牙利算法】poj1469 COURSES && poj2446 Chessboard
- poj 1274最大匹配匈牙利算法
- poj 1469 COURSES 二分图最大匹配 匈牙利算法
- POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)
- poj3020 匈牙利算法+公式:二分无向图的最小路径覆盖 = 顶点数 - 最大二分匹配数 / 2
- POJ1469[COURSES] 二分图最大匹配 匈牙利算法
- POJ-3041 匈牙利算法 二分图最大匹配
- poj 3020 最小边覆盖与最大匹配 匈牙利算法
- 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids
- poj 3041 Asteroids (匈牙利算法---二分图最大匹配)
- poj 3894 System Engineer (二分图最大匹配--匈牙利算法)
- 【二分图|最大匹配】POJ-3041 Asteroids(匈牙利算法dfs、bfs版)
- poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)