Problem 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

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

题解:状压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(动态规划基础)的更多相关文章

  1. Uva10817 Headmaster's Headache

    https://odzkskevi.qnssl.com/b506a3c20adad78678917d1ff4c9b953?v=1508327485 [题解] dp[i][S1][S2]表示前i个教师选 ...

  2. UVA 10817 十一 Headmaster's Headache

    Headmaster's Headache Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Sub ...

  3. UVA 10817 Headmaster's Headache(DP +状态压缩)

    Headmaster's Headache he headmaster of Spring Field School is considering employing some new teacher ...

  4. nyist oj 79 拦截导弹 (动态规划基础题)

    拦截导弹 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...

  5. Problem C: 动态规划基础题目之数字三角形

    Problem C: 动态规划基础题目之数字三角形 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 208  Solved: 139[Submit][Sta ...

  6. 状压DP UVA 10817 Headmaster's Headache

    题目传送门 /* 题意:学校有在任的老师和应聘的老师,选择一些应聘老师,使得每门科目至少两个老师教,问最少花费多少 状压DP:一看到数据那么小,肯定是状压了.这个状态不好想,dp[s1][s2]表示s ...

  7. Codeforces Flipping game 动态规划基础

    题目链接:http://codeforces.com/problemset/problem/327/A 这道题目有O(N^3)的做法,这里转化为动态规划求解,复杂度是O(N) #include < ...

  8. UVA437-The Tower of Babylon(动态规划基础)

    Problem UVA437-The Tower of Babylon Accept: 3648  Submit: 12532Time Limit: 3000 mSec Problem Descrip ...

  9. 《挑战程序设计竞赛》2.3 动态规划-基础 POJ3176 2229 2385 3616 3280

    POJ3176 Cow Bowling 题意 输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线. 规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个 ...

随机推荐

  1. python学习笔记(二)、字符串操作

    该一系列python学习笔记都是根据<Python基础教程(第3版)>内容所记录整理的 1.字符串基本操作 所有标准序列操作(索引.切片.乘法.成员资格检查.长度.最小值和最大值)都适用于 ...

  2. Hibernate入门(七)一对多入门案例

    一对多 场景模拟:用户(一)对订单(多) 1.建表 创建客户表,字段有:客户id,客户姓名,客户性别,客户年龄,客户年纪,客户电话. 创建订单表,字段有:订单编号,明细编号,客户编号(外键) DROP ...

  3. thinkphp模板中,checkbox回显问题

    thinkphp 模板里面可以这样写包含操作 //in 标签 <in name="变量名" value="值1,值2,...">要输出的内容< ...

  4. MVC模式-----struts2框架

    MVC模式-----struts2框架 一.struts MVC模式 1.概述 MVC是模型(model)---视图model(view)---控制器(control)的缩写,是一种用于将逻辑.数据和 ...

  5. CSS gradient渐变之webkit核心浏览器下的使用以及实例

    一.关于渐变 渐变是一种应用于平面的视觉效果,可以从一种颜色逐渐地转变成另外一种颜色,故可以创建类似于彩虹的效果渐变可以应用在任何可以使用图片的地方.例如,您可以指定一个这么一个渐变:顶部的颜色是红色 ...

  6. 2018-06-21 中文代码示例视频演示Python入门教程第五章 数据结构

    知乎原链 续前作: 中文代码示例视频演示Python入门教程第四章 控制流 对应在线文档: 5. Data Structures 这一章起初还是采取了尽量与原例程相近的汉化方式, 但有些语义较偏(如T ...

  7. HTML元素被定义为块级元素或内联元素。那么什么是块级元素,什么是内联元素呢

    块级元素(block)特性: 块级元素在浏览器显示时,通常会以新行来开始(和结束). 宽度(width).高度(height).内边距(padding)和外边距(margin)都可控制;就像以前用到的 ...

  8. Hibernate概念初探

    概述 Hibernate是一个开源代码的对象关系映射(ORM)框架,是基于Java的持久化中间件,它对JDBC进行轻量级的对象封装. 它不仅提供了从Java类到数据表之间的映射,也提供了查询和事务机制 ...

  9. Spring学习之旅(一)极速创建Spring框架java工程项目

    编译工具:eclipse 1)创建java工程:Spring_helloworld 2)导入所需jar包: 3)创建一个实体类: public class HelloBeans { private S ...

  10. ngx-moment汉化

    1.导入汉化文件 import '../../../node_modules/moment/locale/zh-cn.js' 2.使用汉化 <span>{{item.time|amLoca ...