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层中与它位置相邻的两个 ...
随机推荐
- nginx配置虚拟机
在/usr/local/nginx/conf目录下nginx.conf文件是nginx的配置文件. 一.通过端口号区分虚拟机 在nginx.conf文件中添加一个Service节点,修改端口号: se ...
- Netty实战七之EventLoop和线程模型
简单地说,线程模型指定了操作系统.编程语言.框架或者应用程序的上下文中的线程管理的关键方面.Netty的线程模型强大但又易用,并且和Netty的一贯宗旨一样,旨在简化你的应用程序代码,同时最大限度地提 ...
- 并发之AQS
一.概述 谈到并发,不得不谈ReentrantLock:而谈到ReentrantLock,不得不谈AbstractQueuedSynchronizer(AQS)! 类如其名,抽象的队列式的同步器,AQ ...
- js对象工厂函数与构造函数
转自:http://www.cnblogs.com/Jener/p/5920963.html ★概述: 使用对象字面量,或者向空对象中动态地添加新成员,是最简单易用的对象创建方法.然而 ...
- 10个JavaScript常见BUG及修复方法
译者按: JavaScript语言设计太灵活,用起来不免要多加小心掉进坑里面. 原文: Top 10 bugs and their bug fixing 译者: Fundebug 为了保证可读性,本文 ...
- react学习笔记2
1.build文件介绍 (1)react.js 是react的核心库 (2)react-dom.js 提供与DOM相关功能 (3)browser.js 是将JSX语法转为javascript语法 ...
- SAP MM GR-based IV, 无GR不能IV?
SAP MM GR-based IV, 无GR不能IV? 如下的采购订单, 没有做过收货, ITEM的details里却勾选了’GR-Bsd IV’ 选项. 此时试图直接针对该PO#450260713 ...
- iphone屏幕镜像怎么用 手机投屏电脑
手机看视频有的时候总会感觉到累,屏幕太小看的不够爽又或者用手一直拿着手机看累得慌.我就就喜欢看电视因为电视屏幕大看的爽,而且现在很多手机视频都可以往电视上投影视频,那么iphone屏幕镜像怎么用? 使 ...
- 系统调用fork()在powerpc上的源码分析
总结一句话:系统调用的本质,通过sc指令触发异常,完成用户态到内核的转换. 展开一些:应用程序调用fork(),fork()是一个glibc函数,该函数的最底层调用sc指令,触发cpu异常,从而完成从 ...
- Android 直接修改dex破解
直接修改dex破解 一.编写一个简单的验证程序 (1)MainActivity: protected void onCreate(BundlesavedInstanceState) { super.o ...