Problem D: Headmaster's Headache

Time limit: 2 seconds

The headmaster of Spring Field School is considering employing some new teachers for certain subjects. There are a number of teachers applying for the posts. Each teacher is able to teach one or more subjects. The headmaster wants to select applicants so that each subject is taught by at least two teachers, and the overall cost is minimized.

Input

The input consists of several test cases. The format of each of them is explained below:

The first line contains three positive integers SM andNS (≤ 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 SYou 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

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAX_N = ;
const int INF = ;
int S, M, N;
int ns1 = , ns2 = ;
int s[MAX_N], prize[MAX_N];
int dp[ << ][ << ]; void solve() {
dp[ns1][ns2] = prize[];
dp[ns2][ns1] = prize[];
for(int i = ; i <= N; ++i) {
for(int s1 = ( << S) - ; s1 >= ; --s1) {
for(int s2 = ( << S) - ; s2 >= ; --s2) {
if(dp[s1][s2] != INF) {
dp[s1 | s[i]][s1 & s[i] | s2] =
min(dp[s1 | s[i]][s1 & s[i] | s2]
, dp[s1][s2] + prize[i]); dp[s2 & s[i] | s1][s2 | s[i]] =
min(dp[s2 & s[i] | s1][s2 | s[i]]
, dp[s1][s2] + prize[i]);
}
}
}
}
} int main()
{
//freopen("sw.in","r",stdin);
while(~scanf("%d%d%d", &S, &M, &N) && (S + M + N)) {
ns1 = ; ns2 = ;
for(int i = ; i < ( << S); ++i)
for(int j = ; j < ( << S); ++j) dp[i][j] = INF;
memset(prize, , sizeof(prize));
memset(s, , sizeof(s)); for(int i = ; i <= M; ++i) {
int ch, v;
char c;
scanf("%d%d%c",&v, &ch, &c);
prize[] += v;
ns2 |= ns1 & ( << (ch - ));
ns1 |= << (ch - ); while(c != '\n') {
scanf("%d%c", &ch, &c);
ns2 |= ns1 & ( << (ch - ));
ns1 |= << (ch - );
} } for(int i = ; i <= N; ++i) {
int ch;
char c;
scanf("%d%d%c", &prize[i], &ch, &c);
s[i] |= << (ch - );
while(c != '\n') {
scanf("%d%c", &ch, &c);
s[i] |= << (ch - );
}
} solve();
printf("%d\n", dp[( << S) - ][( << S) - ]);
}
return ;
}

uva 10817的更多相关文章

  1. UVA 10817 十一 Headmaster's Headache

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

  2. uva 10817(数位dp)

    uva 10817(数位dp) 某校有m个教师和n个求职者,需讲授s个课程(1<=s<=8, 1<=m<=20, 1<=n<=100).已知每人的工资c(10000 ...

  3. 状压DP UVA 10817 Headmaster's Headache

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

  4. UVa 10817 - Headmaster's Headache(状压DP)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. UVA 10817 - Headmaster's Headache(三进制状压dp)

    题目:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&pag ...

  6. uva 10817(状压dp)

    题意:就是有个学校要招老师.要让没门课至少有两个老师可以上.每个样样例先输入三个数字课程数量s,已经在任的老师数量,和应聘的老师数量.已经在任的一定要聘请. 思路是参考了刘汝佳书上的,关键如何状压. ...

  7. UVa 10817 (状压DP + 记忆化搜索) Headmaster's Headache

    题意: 一共有s(s ≤ 8)门课程,有m个在职教师,n个求职教师. 每个教师有各自的工资要求,还有他能教授的课程,可以是一门或者多门. 要求在职教师不能辞退,问如何录用应聘者,才能使得每门课只少有两 ...

  8. UVa 10817 Headmaster's Headache (状压DP+记忆化搜索)

    题意:一共有s(s ≤ 8)门课程,有m个在职教师,n个求职教师.每个教师有各自的工资要求,还有他能教授的课程,可以是一门或者多门. 要求在职教师不能辞退,问如何录用应聘者,才能使得每门课只少有两个老 ...

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

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

随机推荐

  1. hashCode()和toString()

    hashCode函数和toString函数也在Object类中,同样,所有的类都继承了这2个函数. hashCode函数用于生成哈希码,没有参数,返回值为整型 把u的值作为键存入map中,使用get方 ...

  2. C 基于UDP实现一个简易的聊天室

    引言 本文是围绕Linux udp api 构建一个简易的多人聊天室.重点看思路,帮助我们加深 对udp开发中一些api了解.相对而言udp socket开发相比tcp socket开发注意的细节要少 ...

  3. c++编程规范的纲要和记录

    这是一本好书, 可以让你认清自己对C++的掌握程度.看完之后,给自己打分,我对C++了解多少? 答案是不足20分.对于我自己是理所当然的问题, 就不提了, 记一些有启发的条目和细节: (*号表示不能完 ...

  4. C#之玩转反射【转:http://www.cnblogs.com/yaozhenfa/p/CSharp_Reflection_1.html】

    前言 之所以要写这篇关于C#反射的随笔,起因有两个:   第一个是自己开发的网站需要用到   其次就是没看到这方面比较好的文章. 所以下定决心自己写一篇,废话不多说开始进入正题. 前期准备 在VS20 ...

  5. python数组的使用

    python数组的使用 2010-07-28 17:17 1.Python的数组分三种类型:(1) list 普通的链表,初始化后可以通过特定方法动态增加元素.定义方式:arr = [元素] (2) ...

  6. 关于DataSource的一些记录

    今天看WWDC的232_hd_advanced_user_interfaces_with_collection_views,里面花了一般的时间来讲如何合理的设计程序的datesource,将的很有道理 ...

  7. maven学习手记 - 2

    学习目标 maven 的插件和目标: maven 的生命周期和阶段.   前言 在手记1中看到执行 mvn clean package 时,maven 自动执行了compile 和 test 操作. ...

  8. hi3531播放1080p60f, 延迟越来越大的问题与解决办法

    问题 hi3531播放1080p60f, 延迟越来越大 左边屏幕是ffplay播放的,右边屏幕是3531播放的 数据是udp组播 mpegts, h264 12M码流 原因 经过测试发现: 解码器中缓 ...

  9. asp.net解决高并发的方案.

    asp.net解决高并发的方案. Posted on 2012-11-27 22:31 75077027 阅读(3964) 评论(1) 编辑 收藏 最近几天一直在读代震军的博客,他是 Discuz!N ...

  10. 自定义debug信息

    #ifdef  DEBUG  #define debug(fmt,args...)  printk(fmt ,##args)  #define debugX(level,fmt,args...) if ...