uva 10817

Problem D: Headmaster's Headache

Time limit: 2 seconds

The headmaster of Spring Field School is considering employing some new teachers for certain subjects. There are a number of teachers applying for the posts. Each teacher is able to teach one or more subjects. The headmaster wants to select applicants so that each subject is taught by at least two teachers, and the overall cost is minimized.

 

Input

The input consists of several test cases. The format of each of them is explained below:

The first line contains three positive integers SM andNS (≤ 8) is the number of subjects, M (≤ 20) is the number of serving teachers, and N (≤ 100) is the number of applicants.

Each of the following M lines describes a serving teacher. It first gives the cost of employing him/her (10000 ≤ C ≤ 50000), followed by a list of subjects that he/she can teach. The subjects are numbered from 1 to SYou must keep on employing all of them.After that there are N lines, giving the details of the applicants in the same format.

Input is terminated by a null case where S = 0. This case should not be processed.

 

Output

For each test case, give the minimum cost to employ the teachers under the constraints.

 

Sample Input

2 2 2
10000 1
20000 2
30000 1 2
40000 1 2
0 0 0

 

Sample Output

60000

 状态dp

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 const int MAX_N = 105;
 9 const int INF = 7000000;
10 int S, M, N;
11 int ns1 = 0 , ns2 = 0;
12 int s[MAX_N], prize[MAX_N];
13 int dp[1 << 9][1 << 9];
14 
15 void solve() {
16         dp[ns1][ns2] = prize[0];
17         dp[ns2][ns1] = prize[0];
18         for(int i = 1; i <= N; ++i) {
19                 for(int s1 = (1 << S) - 1; s1 >= 0; --s1) {
20                         for(int s2 = (1 << S) - 1; s2 >= 0; --s2) {
21                                 if(dp[s1][s2] != INF) {
22                                         dp[s1 | s[i]][s1 & s[i] | s2] =
23                                         min(dp[s1 | s[i]][s1 & s[i] | s2]
24                                         , dp[s1][s2] + prize[i]);
25 
26                                         dp[s2 & s[i] | s1][s2 | s[i]] =
27                                         min(dp[s2 & s[i] | s1][s2 | s[i]]
28                                         , dp[s1][s2] + prize[i]);
29                                 }
30                         }
31                 }
32         }
33 }
34 
35 int main()
36 {
37     //freopen("sw.in","r",stdin);
38     while(~scanf("%d%d%d", &S, &M, &N) && (S + M + N)) {
39             ns1 = 0; ns2 = 0;
40             for(int i = 0; i < (1 << S); ++i)
41                     for(int j = 0; j < (1 << S); ++j) dp[i][j] = INF;
42             memset(prize, 0, sizeof(prize));
43             memset(s, 0, sizeof(s));
44 
45             for(int i = 1; i <= M; ++i) {
46                     int ch, v;
47                     char c;
48                     scanf("%d%d%c",&v, &ch, &c);
49                     prize[0] += v;
50                     ns2 |=  ns1 & (1 << (ch - 1));
51                     ns1 |= 1 << (ch - 1);
52 
53                     while(c != '\n') {
54                             scanf("%d%c", &ch, &c);
55                             ns2 |=  ns1 & (1 << (ch - 1));
56                             ns1 |= 1 << (ch - 1);
57                     }
58 
59             }
60 
61             for(int i = 1; i <= N; ++i) {
62                     int ch;
63                     char c;
64                     scanf("%d%d%c", &prize[i], &ch, &c);
65                     s[i] |=  1 << (ch - 1);
66                     while(c != '\n') {
67                             scanf("%d%c", &ch, &c);
68                             s[i] |= 1 << (ch - 1);
69                     }
70             }
71 
72             solve();
73             printf("%d\n", dp[(1 << S) - 1][(1 << S) - 1]);
74     }
75     return 0;
76 }
View Code

 

posted @ 2014-05-12 21:31  hyx1  阅读(242)  评论(1编辑  收藏  举报