poj 2584 T-Shirt Gumbo(多重匹配)

题目来源:http://acm.pku.edu.cn/JudgeOnline/problem?id=2584

这是一道典型的多重匹配的题目,纯以模板即可AC。

多重匹配仅仅是比最大匹配多了一点变化而已,但其思想是完全不变的,都是在不停地找增广路,并随时更新 link 值。

下面是我的代码:

 

 

代码
   
     
1 #include < iostream >
2 #include < algorithm >
3   using namespace std;
4
5   const int Maxn = 105 ;
6 int n;
7 int g[Maxn][ 2 ], num[ 10 ], link[ 10 ][Maxn];
8 bool used[ 10 ];
9 char str[ 15 ];
10
11 int Search( char c)
12 {
13 if (c == ' S ' ) return 1 ;
14 if (c == ' M ' ) return 2 ;
15 if (c == ' L ' ) return 3 ;
16 if (c == ' X ' ) return 4 ;
17 return 5 ;
18 }
19
20 bool find( int x)
21 {
22 for ( int i = g[x][ 0 ]; i <= g[x][ 1 ]; i ++ )
23 {
24 if ( ! used[i])
25 {
26 used[i] = true ;
27 if (num[i])
28 {
29 link[i][ ++ link[i][ 0 ]] = x;
30 num[i] -- ;
31 return true ;
32 }
33 else
34 {
35 for ( int j = 1 ; j <= link[i][ 0 ]; j ++ )
36 {
37 if (find(link[i][j]))
38 {
39 link[i][j] = x;
40 return true ;
41 }
42 }
43 }
44 }
45 }
46
47 return false ;
48 }
49
50 int main()
51 {
52 while (scanf( " %s " ,str) != EOF)
53 {
54 if (strcmp(str, " ENDOFINPUT " ) == 0 )
55 break ;
56 scanf( " %d " , & n);
57 for ( int i = 1 ; i <= n; i ++ )
58 {
59 scanf( " %s " ,str);
60 int k1 = Search(str[ 0 ]);
61 int k2 = Search(str[ 1 ]);
62 g[i][ 0 ] = k1, g[i][ 1 ] = k2;
63 }
64 for ( int i = 1 ; i <= 5 ; i ++ )
65 scanf( " %d " , & num[i]);
66 scanf( " %s " ,str);
67
68 int ans = 0 ;
69 memset(link, 0 , sizeof (link));
70 for ( int i = 1 ; i <= n; i ++ )
71 {
72 memset(used, false , sizeof (used));
73 if (find(i))
74 ans ++ ;
75 }
76
77 if (ans == n)
78 printf( " T-shirts rock!\n " );
79 else
80 printf( " I'd rather not wear a shirt anyway...\n " );
81 }
82 return 0 ;
83 }
84

 

你可能感兴趣的:(poj)