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层中与它位置相邻的两个 ...
随机推荐
- 1. 七种join的sql编写
一.join图 二.sql演示 a.创建演示表及数据 SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ----------------------- ...
- 接触Java23天
根据老师的要求写了一段然后在评讲的时候在修该一些: 猫的: public class Cat extends Animal{ public void methodCat(){ System.out.p ...
- phpcms有二级导航并且高亮效果代码
<div class="collapse navbar-collapse" id="example-navbar-collapse"> <ul ...
- leaflet 如何绘制圆
方法1(根据指定的半径和中心点去绘制圆) var polygon1 = new L.Circle([34, 108], 120000, { color: 'red', //颜色 fillColor: ...
- View在测量时的MeasureSpec由什么决定?
我们都知道系统要确定View的大小,首先得先获得MeasureSpec,再通过MeasureSpec来决定View的大小. MeasureSpec(32为int值)由两部分组成: SpecMode(高 ...
- (转载)解决NVIDIA显卡驱动“没有找到兼容的图形硬件”的问题
(转载)解决NVIDIA显卡驱动“没有找到兼容的图形硬件”的问题 原出处:http://www.cnblogs.com/longdouhzt/archive/2012/02/28/2370660.ht ...
- 产品经理说|AIOps 让告警管理变得更智能
AIOps 人工智能和IT运营支撑 Ops 之间的故事,愈演愈烈,已经成为当今运维圈的热门话题,我打算从2篇文档分享我们在 AIOps 上一些探索和实践.(本篇)为什么事件(告警)处理需要 AIOps ...
- 「客户成功故事」OneAPM 助力网上办事大厅构建阳光、高效、安全的政务服务平台
(一) 项目背景: 网上办事大厅是由省信息中心承建的电子政务核心业务系统,致力于为全省民众提供一站式网上办事服务,实现了政务信息网上公开.法人及个人事项网上办理.公共决策网上互动.政府效能网上监督五大 ...
- 漫说996icu黑名单
以实际行动声援996icu项目. https://github.com/996icu/996.ICU/blob/master/blacklist/blacklist.md 996公司黑名单,京东,华为 ...
- NoSQL与MongoDB介绍
写在前面 本文是由一次演讲整理出来的,文中大部分资料来源于网络,感谢Wikipedia,Google和MongoDB官网.文中使用的MongoDB版本为1.2.4. What is NoSQL NoS ...