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 S, M andN. S (≤ 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 S. You 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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAX_N = ;
const int INF = ;
int S, M, N;
int ns1 = , ns2 = ;
int s[MAX_N], prize[MAX_N];
int dp[ << ][ << ]; void solve() {
dp[ns1][ns2] = prize[];
dp[ns2][ns1] = prize[];
for(int i = ; i <= N; ++i) {
for(int s1 = ( << S) - ; s1 >= ; --s1) {
for(int s2 = ( << S) - ; s2 >= ; --s2) {
if(dp[s1][s2] != INF) {
dp[s1 | s[i]][s1 & s[i] | s2] =
min(dp[s1 | s[i]][s1 & s[i] | s2]
, dp[s1][s2] + prize[i]); dp[s2 & s[i] | s1][s2 | s[i]] =
min(dp[s2 & s[i] | s1][s2 | s[i]]
, dp[s1][s2] + prize[i]);
}
}
}
}
} int main()
{
//freopen("sw.in","r",stdin);
while(~scanf("%d%d%d", &S, &M, &N) && (S + M + N)) {
ns1 = ; ns2 = ;
for(int i = ; i < ( << S); ++i)
for(int j = ; j < ( << S); ++j) dp[i][j] = INF;
memset(prize, , sizeof(prize));
memset(s, , sizeof(s)); for(int i = ; i <= M; ++i) {
int ch, v;
char c;
scanf("%d%d%c",&v, &ch, &c);
prize[] += v;
ns2 |= ns1 & ( << (ch - ));
ns1 |= << (ch - ); while(c != '\n') {
scanf("%d%c", &ch, &c);
ns2 |= ns1 & ( << (ch - ));
ns1 |= << (ch - );
} } for(int i = ; i <= N; ++i) {
int ch;
char c;
scanf("%d%d%c", &prize[i], &ch, &c);
s[i] |= << (ch - );
while(c != '\n') {
scanf("%d%c", &ch, &c);
s[i] |= << (ch - );
}
} solve();
printf("%d\n", dp[( << S) - ][( << S) - ]);
}
return ;
}