uva10817(注意输入的解决)
来源:互联网 发布:儿童 绘画 推荐 知乎 编辑:程序博客网 时间:2024/06/10 23:39
题目描述:
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
题意:
某校有n个教师和m个求职者,需要讲授s个课程(1<=s<=8,1<=m<=20,1<=n<=100).已知每人的工资c(10000<=c<=50000)和能教的课程集合,要求支付最少的工资使得每门课都至少有两名教师教学。在职教师不能辞退。
分析:
状态压缩dp,0-1背包问题,对于求职者要么选要么不选,用两个集合s1表示恰好有一个人教的科目集合,s2表示至少有两个人教的科目的集合,而d[i][s1][s2]表示前i个人的最小花费。状态转移方程为d[i][s1][s2]=min(dp(i+1,s0,s1,s2),a[i]+dp(i+1,s0,s1,s2))(当i>=m时)。
最终的结果是dp(0,1<<(s-1),0,0)。
附刘汝佳代码:
#include<cstdio>#include<cstring>#include<iostream>#include<sstream>using namespace std;const int maxn = 100 + 20 + 5;const int maxs = 8;const int INF = 1000000000;int m, n, s, c[maxn], st[maxn], d[maxn][1<<maxs][1<<maxs];// s1是一个人教的科目集合,s2是两个人教的科目集合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) ans = dp(i+1, s0, s1, s2); // 不选 // 选 int m0 = st[i] & s0, m1 = st[i] & s1; s0 ^= m0; s1 = (s1 ^ m1) | m0; s2 |= m1; ans = min(ans, c[i] + dp(i+1, s0, s1, s2)); return ans;}int main() { int x; string line; while(getline(cin, line)) { stringstream ss(line); ss >> s >> m >> n; if(s == 0) break; for(int i = 0; i < m+n; i++) { getline(cin, line); stringstream ss(line); ss >> c[i]; st[i] = 0; while(ss >> x) st[i] |= (1 << (x-1)); } memset(d, -1, sizeof(d)); cout << dp(0, (1<<s)-1, 0, 0) << "\n"; } return 0;}
- uva10817(注意输入的解决)
- uva10817
- UVa10817
- uva10817
- Uva10817
- UVa10817
- 字符串的输入注意
- UVA10817-----Headmaster's Headache-----状态压缩的背包(记忆化搜索实现)
- 例题9-15 校长的烦恼 UVa10817
- uva10817 校长的烦恼 利用-ans
- uva10817 - Headmaster's Headache(01背包)
- UVA10817:Headmaster's Headache(状压dp)
- Ternary Calculation(也不用栈,注意输入方式即可,直接解决)
- 解决乱码(注意)
- UVA10817 Headmaster's Headache 状态压缩的01背包
- <frameset>使用注意以及页面输入乱码解决
- 输入模块中该注意的知识点:
- 在输入C++输入字符时应当注意的
- 回归基础系列-冒泡排序-[JAVA]
- 用Meteor 写个即时聊天app
- debian/ubuntu安装virtualbox5.0+vmware12+genymotion2.0模拟器,解决依赖包的问题
- sql 日期时间函数+格式转换
- 回归基础系列-插入排序[JAVA]
- uva10817(注意输入的解决)
- 【BZOJ1062】[NOI2008]糖果雨【二维树状数组】
- 欢迎使用CSDN-markdown编辑器
- php如何实现上一页下一页的功能 (不是列表页 是内容页)
- 《Linux操作系统分析》之分析Linux内核创建一个新进程的过程
- 基于easyui的 增 删 改 查
- C++指针【数组、字符串作为函数的参数】
- 第二次上机作业
- ubuntu新建用户与无密码远程登陆