题意:有m个教师,n个求职者,需讲授s个课程,已知工资c和能教的课程集合,要求支付最少使每门课至少有两名教师,在职教师不能辞退。
分析:枚举,两种情况,选和不选求职者。
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<sstream>
using namespace std;
const int maxn = 100 + 20 + 5;
const int maxs = 8;
const int INF = 1000000000;
int m, n, s, c[maxn], st[maxn], d[maxn][1 << maxs][1 << maxs];
int dp(int i, int s0, int s1, int s2) {
if (i == m + n)return s2 == (1 << s) - 1 ? 0 : INF;
int &ans = d[i][s1][s2];
if (ans >= 0)return ans;
ans = INF;
if (i >= m)ans = dp(i+1, s0, s1, s2);
int m0 = s0 & st[i], m1 = s1 & st[i];
s0 ^= m0;//移除出现的课程
s1 = (s1^m1) | m0;//m0相当于第一次出现的课程,满足两次出现,才能加入s2中
s2 |= m1;//将第2次出现的课程加入s2
ans = min(ans, c[i] + dp(i+1, s0, s1, s2));
return ans;
}
int main() {
int x;
string line;
while (getline(cin, line)) {
stringstream ss(line);
ss >> s >> m >> n;
if (s == 0) break;
for (int i = 0; i < m + n; i++) {
getline(cin, line);
stringstream ss(line);
ss >> c[i];
st[i] = 0;
while (ss >> x) st[i] |= (1 << (x - 1));
}
memset(d, -1, sizeof(d));
cout << dp(0, (1 << s) - 1, 0, 0) << "\n";
}
return 0;
}