题意:s门课,m个现有老师,n个应聘者,每个人给出工资和能教的课,每个人能同时教多门课,每门课至少需要两人教,求招聘需要的最少工资。
题解:状压dp
s
0
s0
s0表示0人教课程集合,
s
1
s1
s1表示1人教课程集合,
s
2
s2
s2表示2人教课程集合。
因为每位老师可以同时教多门课,要花费最小,那就全教。
用
d
p
[
i
]
[
s
1
]
[
s
2
]
dp[i][s1][s2]
dp[i][s1][s2]存储当前状态的最小花费(知道s1、s2,s0就知道了,所有不需要记录),记忆化搜索,过程见注释。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = (1 << 8) + 10;
int s, m, n, co[222], temp, st[222], dp[222][maxn][maxn];
string str;
int solve(int i, int s0, int s1, int s2) {
if (i == m + n + 1) {
if (s2 == (1 << s) - 1) return 0; //所有课都有2人教
else return inf;
}
int& ans = dp[i][s1][s2];
if (ans >= 0) return ans; //存在就返回
ans = inf;
if (i > m) ans = solve(i + 1, s0, s1, s2); //跳过当前应聘者
int m0 = st[i] & s0; //对于0人教的课可以教哪些
int m1 = st[i] & s1; //对于1人教的课可以教哪些
s0 ^= m0; //删除
s1 = (s1 ^ m1) | m0; //删除再加上之前0人教的
s2 |= m1; //加上之前1人教的
ans = min(ans, co[i] + solve(i + 1, s0, s1, s2));
return ans;
}
int main() {
while (~scanf("%d%d%d", &s, &m, &n) && s) {
getchar(); //!!!
memset(st, 0, sizeof(st));
for (int i = 1; i <= m + n; i++) {
getline(cin, str);
stringstream ss(str);
ss >> co[i];
while (ss >> temp) {
st[i] |= (1 << (temp - 1));
}
}
memset(dp, -1, sizeof(dp));
printf("%d\n", solve(1, (1 << s) - 1, 0, 0));
}
return 0;
}