第六十一题 UVA10817 校长的烦恼 Headmaster's Headache

116 篇文章 1 订阅
102 篇文章 1 订阅

The headmaster of Spring Field School is considering employing some new teachers for certain subjects. There are a number of teachers applying for the posts. Each teacher is able to teach one or more subjects. The headmaster wants to select applicants so that each subject is taught by at least two teachers, and the overall cost is minimized. Input The input consists of several test cases. The format of each of them is explained below: The first line contains three positive integers S, M and N. S (≤ 8) is the number of subjects, M (≤ 20) is the number of serving teachers, and N (≤ 100) is the number of applicants. Each of the following M lines describes a serving teacher. It first gives the cost of employing him/her (10000 ≤ C ≤ 50000), followed by a list of subjects that he/she can teach. The subjects are numbered from 1 to S. You must keep on employing all of them. After that there are N lines, giving the details of the applicants in the same format. Input is terminated by a null case where S = 0. This case should not be processed. Output For each test case, give the minimum cost to employ the teachers under the constraints. Sample Input 2 2 2 10000 1 20000 2 30000 1 2 40000 1 2 0 0 0 Sample Output 60000

 

用两个集合,s1表示恰好有一个人教的科目,s2表示至少有两个人教的科目。d(i,s1,s2),表示考虑了前i个人时的最小花费,1-m必须全选上,m+1到你-m才有可能出现选或者不选的

决策,状态转移方程:d(i,s1,s2)=min{d(i+1,s1,s2),fe[i] + d(i+1,s1‘,s2’)},第一项表示不聘用,还是以现在的s1,s2进入第i+1个人,第二项表示聘用,并且更新状态。

//很久没有写过状态压缩DP了  之前就不太会    这次复习代码还是看的刘老师思路
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#define Maxn 205
#define INF 100000000
using namespace std;
int s,m,n;
int fe[Maxn],st[Maxn],dp[150][1<<8][1<<8];

int DP(int i,int s1,int s2) {
	if(i == m + n + 1) return s2 == (1 << s) - 1 ? 0 : INF;
	int &ans = dp[i][s1][s2];
	if(ans >= 0) return ans;
	ans = INF;
	if(i >= m + 1) ans = DP(i+1,s1,s2);// 不聘用 
	int m1 = st[i] & s1;
	s1 = st[i] ^ s1;
	s2 |= m1;
	ans = min(ans,fe[i] + DP(i + 1,s1,s2));//和聘用取min 
	return ans;
}

int main(int argc,char* argv[]) {
	string line; 
	while(getline(cin,line)) {
		stringstream ccin(line);
		ccin >> s >> m >> n;
		if(s == 0) break;
		for(int i=1; i<=m+n; i++){
			getline(cin,line);
			stringstream ccin(line);
			ccin >> fe[i]; int x;
			st[i] = 0;
			while(ccin >> x) st[i] |= (1 << (x - 1));
		}
		memset(dp,-1,sizeof(dp));
		cout << DP(1,0,0)<<endl;
	}
	
	
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值