UVA10817-Headmaster's Headache(动态规划基础)
Time Limit: 4500 mSec
Problem Description

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
Sample Input
Sample Output
60000
题解:状压dp,但是比较麻烦的地方在于不少于2人,因此最直接的想法就是三进制枚举,不过三进制写起来有些麻烦,转念一想,其实可以用两个二进制位来代替这个三进制,我的两个二进制位的状态定义其实有些问题,比较正的思路就是前八位和后八位分别代表一个和大于等于两个。我的代码是跟着lrj的思路做的,实现的思路还是很好的,三进制可以转成两个二进制,用两个集合来表示整个状态,很值得学习(好写嘛),状态定义为dp[i][s1][s2],表示考虑到前i个人,在状态(s1,s2)之下到达目标状态还要花费多少。这样方程就很好写了。在转移状态的时候需要位运算,这个硬想是很困难的,画个图就很简单了,交,并,对称差分别对应&,|,^。
#include <bits/stdc++.h> using namespace std; const int maxs = , maxm = , maxn = ;
const int INF = 1e9; int s, m, n;
int cost[maxn + maxm], teach[maxn + maxm];
int dp[maxm + maxn][ << maxs][ << maxs]; int DP(int i, int s0, int s1, int s2) {
if (i == m + n) return s2 == ( << s) - ? : INF;
int& ans = dp[i][s1][s2];
if (ans >= ) return ans; ans = INF;
if (i >= m) ans = DP(i + , s0, s1, s2); int t0 = s0 & teach[i]; //new subjects
int t1 = s1 & teach[i];
s0 ^= t0; s1 = (s1^t1) | t0; s2 |= t1;
ans = min(ans, DP(i + , s0, s1, s2) + cost[i]);
return ans;
} int main()
{
//freopen("input.txt", "r", stdin);
string str;
while (getline(cin, str)) {
stringstream ss(str);
ss >> s >> m >> n;
if (s == ) break;
memset(teach, , sizeof(teach));
memset(dp, -, sizeof(dp));
for (int i = ; i < n + m; i++) {
getline(cin, str);
stringstream ss(str);
ss >> cost[i];
int t;
while (ss >> t) {
t--;
teach[i] |= ( << t);
}
}
printf("%d\n", DP(, ( << s) - , , ));
}
return ;
}
UVA10817-Headmaster's Headache(动态规划基础)的更多相关文章
- Uva10817 Headmaster's Headache
https://odzkskevi.qnssl.com/b506a3c20adad78678917d1ff4c9b953?v=1508327485 [题解] dp[i][S1][S2]表示前i个教师选 ...
- UVA 10817 十一 Headmaster's Headache
Headmaster's Headache Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Sub ...
- UVA 10817 Headmaster's Headache(DP +状态压缩)
Headmaster's Headache he headmaster of Spring Field School is considering employing some new teacher ...
- nyist oj 79 拦截导弹 (动态规划基础题)
拦截导弹 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...
- Problem C: 动态规划基础题目之数字三角形
Problem C: 动态规划基础题目之数字三角形 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 208 Solved: 139[Submit][Sta ...
- 状压DP UVA 10817 Headmaster's Headache
题目传送门 /* 题意:学校有在任的老师和应聘的老师,选择一些应聘老师,使得每门科目至少两个老师教,问最少花费多少 状压DP:一看到数据那么小,肯定是状压了.这个状态不好想,dp[s1][s2]表示s ...
- Codeforces Flipping game 动态规划基础
题目链接:http://codeforces.com/problemset/problem/327/A 这道题目有O(N^3)的做法,这里转化为动态规划求解,复杂度是O(N) #include < ...
- UVA437-The Tower of Babylon(动态规划基础)
Problem UVA437-The Tower of Babylon Accept: 3648 Submit: 12532Time Limit: 3000 mSec Problem Descrip ...
- 《挑战程序设计竞赛》2.3 动态规划-基础 POJ3176 2229 2385 3616 3280
POJ3176 Cow Bowling 题意 输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线. 规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个 ...
随机推荐
- SpringBoot中异步请求和异步调用(看这一篇就够了)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10661591.html,否则将追究法律责任!!! 一.SpringBoot中异步请求的使用 ...
- Https协议报错:com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl解决方法
旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/82220333 所用应用服务器:JBoss服务 ...
- javascript算法-插入排序
原理跟java那篇文章一样,只是语言不同而已 var InsertSort = function( _n ){ this.maxSize = _n; this.arr = []; this.init ...
- [js高手之路]深入浅出webpack教程系列1-安装与基本打包用法和命令参数
[js高手之路]深入浅出webpack教程系列索引目录: [js高手之路]深入浅出webpack教程系列1-安装与基本打包用法和命令参数 [js高手之路]深入浅出webpack教程系列2-配置文件we ...
- 什么是Docker Volume?
摘要:Docker Volume,通常翻译为数据卷,用于保存持久化数据.当我们将数据库例如MySQL运行在Docker容器中时,一般将数据通过Docker Volume保存在主机上,这样即使删除MyS ...
- [转载]Web Service到底是什么
转自:http://blog.csdn.net/wooshn/article/details/8069087/ 武僧的专栏 一.序言 大家或多或少都听过WebService(Web服务),有一段时间 ...
- js-clickNumCount.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- loj#2483. 「CEOI2017」Building Bridges(dp cdq 凸包)
题意 题目链接 Sol \[f[i], f[j] + (h[i] - h[j])^2 + (w[i - 1] - w[j]))\] 然后直接套路斜率优化,发现\(k, x\)都不单调 写个cdq就过了 ...
- JS检测浏览器Adobe Reader插件
Web应用中当我们希望向用户显示pdf文档时候,如果用户安装了Adobe Reader之类的pdf阅读器,就可以直接打开文档在浏览器中显示, 但是,当用户没有安装这类软件的时候,自然是打不开的,为了系 ...
- MachineLN博客目录
MachineLN博客目录 https://blog.csdn.net/u014365862/article/details/78422372 本文为博主原创文章,未经博主允许不得转载.有问题可以加微 ...