题意:
某校有m个教师和n个求职者,需讲授s个课程,已知每人的工资c和能教的课程集合,要求支付最少的工资使得每门课都至少有两名老师能教,在职老师不能辞退。
思路:
定义状态: d[ i ][s0][s1][s2]: 前 i 位老师在当前状态下(s0,s1,s2)的最小花费。
其中,s0:0人代课的科目集合; s1:1人代课的科目集合; s2:至少2人代课的科目集合 。
s0可以由s1和s2推出,所以d数组可以降一个维度。
转移方程: d[i][s1][s2] = min( d[i][s1’][s2’] + cost[i] ,d[i][s1][s2] )。
min里面第一项代表选第 i 位老师,第二项代表不选。第二项只有当i >= m时才有,因为题目里在职老师不能辞退。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 1<<30;
const int maxn = 120+5;
const int maxs = 8;
int cost[maxn], teach[maxn], d[maxn][1<<maxs][1<<maxs];
int s,m,n;// s:课程数;m:老师数;n:应聘者数
// cur:当前老师; s0:0人代课的科目集合; s1:1人代课的科目集合; s2:至少2人代课的科目集合
int solve(int cur, int s0, int s1, int s2){
if(cur == m+n) return s2 == (1<<s)-1? 0 : INF;
int& ans = d[cur][s1][s2];
if(ans >= 0) return ans;
ans = INF;
if(cur >= m) ans = solve(cur+1, s0, s1, s2); // 不选
int m0 = teach[cur]&s0, m1 = teach[cur]&s1;
s0^= m0; s1 = (s1^m1)|m0; s2|= m1;
ans = min(ans, cost[cur] + solve(cur+1, s0, s1, s2)); // 选
return ans;
}
void input(int s, int e){
for(int i = s; i < e; ++i){
scanf("%d",&cost[i]);
char ch;
while(ch = getchar()!='\n'){
int a; scanf("%d",&a);
teach[i]+= (1<<(a-1));
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d %d %d",&s,&m,&n) == 3&&n){
memset(teach,0,sizeof(teach));
memset(d,-1,sizeof(d));
input(0,m); input(m,m+n);
int ans = solve(0, (1<<s)-1, 0, 0);
printf("%d\n",ans);
}
return 0;
}