题目大意
每个学校有n个教师和m个求职者。已经知道每个的工资和能教的课程集合,要求支付最少的工资使得某门课都哦至少有两名教师教 m<20,s<8;
分析
当数据量小的时候首先考虑状压dp
因为每门课程有至少有两名老师来教,因此用一个二进制集合难以满足要求,但是用三进制又是不可能的,因此我们需要两个值s1,s2来表示课程集合。状态就可以用三个量来表示了
d(i,s1,s2) 表示前i个老师任课后花费最少的钱。貌似不错,但是细想就会发现一些问题,状态转移方程无法下手,因为如果前i个老师的任课已被确定,但是后面却有能教相同课程的花费更少的老师会没有办法被聘用,于是我们引入一个量s0表示至今已经被教的课程
理解错了题意:题意表示前n个教师一定会被用,不管他们教了多少课,让他们物尽其用.用完n个教师之后还剩多少课,让剩下的求职者去教,这样就相当于0-1背包问题了,因为每个求职者只有聘用和不聘用两种状态
#include<iostream>
#include<algorithm>
#include<string.h>
#include<sstream>
#include<string>
#define maxn 200
#define maxs 10
#define Inf 0x3f3f3f3f
using namespace std;
int s, m, n;
int c[maxn],q[maxn];
int 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) //不用第i+1名求职者
ans = dp(i + 1, s0, s1, s2);
int m0 = s0 & q[i];
int m1 = s0 & q[i];
s0 ^= m0;
s1 = (s1^m1) | m0;
s2 |= m1;
ans = min(ans, c[i] + dp(i + 1, s0, s1, s2)); //比较
return ans;
}
int main()
{
while (cin >> s >> m >> n&&s&&n&&m) {
memset(d, -1, sizeof(d));
memset(q, 0, sizeof(q));
getchar();
for (int i = 0; i < n + m; i++) {
string str;
getline(cin, str);
stringstream ss(str);
int x, flag = 1;
while (ss >> x)
{
if (flag) { flag = 0; c[i] = x; }
else {
x--;
q[i] |= (1 << x);
}
}
}
int ans = dp(0, (1 << s) - 1, 0, 0);
cout << ans << endl;
}
return 0;
}