Poj--2584(二分图最大匹配,多重匹配)

2014-11-04 21:31:10

思路:不造什么叫多重匹配,反正按照正常的思路就能过了。

  考虑每个参赛者,假设他要S、M、L的衣服,那么就把他和所有S、M、L的衣服连边(有几件衣服练几条边),然后就是最大匹配了。

 1 /*************************************************************************
 2     > File Name: 2584.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Tue 04 Nov 2014 07:12:09 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <map>
14 #include <set>
15 #include <stack>
16 #include <queue>
17 #include <iostream>
18 #include <algorithm>
19 using namespace std;
20 #define lp (p << 1)
21 #define rp (p << 1|1)
22 #define getmid(l,r) (l + (r - l) / 2)
23 #define MP(a,b) make_pair(a,b)
24 typedef long long ll;
25 const int INF = 1 << 30;
26 const int maxn = 30;
27 
28 char str[100],sz[maxn][10];
29 char stand[10] = {'\0','S','M','L','X','T'};
30 int first[maxn],next[maxn * 110],ver[maxn * 110],ecnt;
31 int mat[110],used[110];
32 int X,num[10];
33 
34 void Init(){
35     memset(first,-1,sizeof(first));
36     ecnt = 0;
37 }
38 
39 void Add_edge(int u,int v){
40     next[++ecnt] = first[u];
41     ver[ecnt] = v;
42     first[u] = ecnt;
43 }
44 
45 bool find(int p){
46     for(int i = first[p]; i != -1; i = next[i]){
47         int v = ver[i];
48         if(used[v] == 0){
49             used[v] = 1;
50             if(mat[v] == 0 || find(mat[v])){
51                 mat[v] = p;
52                 return true;
53             }
54         }
55     }
56     return false;
57 }
58 
59 int Hungary(){
60     int ans = 0;
61     memset(mat,0,sizeof(mat));
62     for(int i = 1; i <= X; ++i){
63         memset(used,0,sizeof(used));
64         if(find(i)) ++ans;
65     }
66     return ans;
67 }
68 
69 int main(){
70     while(scanf("%s",str) != EOF){
71         if(strcmp(str,"ENDOFINPUT") == 0)
72             break;
73         scanf("%d",&X);
74         Init();
75         for(int i = 1; i <= X; ++i)
76             scanf("%s",sz[i]);
77         for(int i = 1; i <= 5; ++i)
78             scanf("%d",num + i);
79         for(int i = 1; i <= X; ++i){
80             int id1,id2;
81             for(id1 = 1; id1 <= 5; ++id1)
82                 if(sz[i][0] == stand[id1]) break;
83             for(id2 = 1; id2 <= 5; ++id2)
84                 if(sz[i][1] == stand[id2]) break;
85             for(int j = id1; j <= id2; ++j){
86                 for(int k = 1; k <= num[j]; ++k){
87                     Add_edge(i,(j - 1) * 20 + k);
88                 }
89             }
90         }
91         scanf("%s",str);
92         if(Hungary() == X) printf("T-shirts rock!\n");
93         else printf("I'd rather not wear a shirt anyway...\n");
94     }
95     return 0;
96 }

 

posted @ 2014-11-04 21:36  Naturain  阅读(152)  评论(0编辑  收藏  举报