UVa10817校长的烦恼

题意:

某校有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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值