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层中与它位置相邻的两个 ...
随机推荐
- 10. Condition 控制线程通信
1. 是什么 ? 2. 示例 package com.gf.demo09; import java.util.concurrent.locks.Condition; import java.util. ...
- JavaWeb-BeginTomcat
上手Tomcat 1.Ubuntu 18.04 下载/安装Tomcat 以下内容参考链接 安装JDK sudo apt-get update sudo apt-get install default- ...
- JavaScript黑客是这样窃取比特币的,Vue开发者不用担心!
如果你是JavaScript或者区块链开发者,如果你有关注区块链以及比特币,那么你应该听说了比特币钱包Copay被黑客攻击的事情.但是,你知道这是怎么回事吗? 总结 比特币钱包copay依赖event ...
- es6 语法 (iterator和for...of循环)
Iterator遍历器 遍历器(Iterator)就是这样一种机制.它是一种接口,为各种不同的数据结构提供统一的访问机制.任何数据结构只要部署Iterator接口,就可以完成遍历操作(即依次处理该数据 ...
- 【代码笔记】Web-Javascript-Javascript typeof
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- win10怎么录制电脑屏幕 电脑播放视频录制
随着社会的发展,网络信息化时代已经来临,作为一个上班族,每天都离不开电脑,电脑仿佛就是我们的合作伙伴,也是陪伴我们的朋友,如今win10系统已经出来了,关于win10系统的问题相信大家有很多的问题,今 ...
- 如何用ABP框架快速完成项目(4) - 如何正确使用ABP?
正如我在<如何用ABP框架快速完成项目(2) - 快的定义!>提到的, 很多同学在使用ABP中遇到很多问题, 花了很多时间和精力, 然而从最根本的角度和方向上来看这些问题应该是不存在. 这 ...
- iOS------自动查找项目中不用的图片资源
注意:删除的时候要谨慎!别什么图都删了,看看对项目有没有作用.这个插件有时也会有一定的误差. 具体操作步骤: 1.去github上下载LSUnusedResources(下载地址:https://gi ...
- (网页)JavaScript周末总结(一)
本周学习的内容总结: 1. 2章在html中使用javascript. 2. 3章Javascript的基本概念. 3. 4章变量,作用域,内存问题. 1-1:2章以下内容: 1.包含javascri ...
- HttpWebRequest 请求带OAuth2 授权的webapi
OAuth 2.0注意事项: 1. 获取access_token时,请使用POST private static string GetAuthorization(string username, st ...