Headmaster's Headache UVa10817【DP】(缺)

题目:

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 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



意思:

某校有m个教师和n个求职者,需要教授s个课程(1<=s<=8,1<=m<=20,1<=n<=100).已知每人的工资c(10000<=c<=50000)和能教的课程集合,要求支付最少的工资使每门课都至少有两名教师能教。在职教师不能辞退。

之后补充做法

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<sstream>
#define maxn 150
#define INF 0X3F3F3F3F
using namespace std;  

int S,M,N;
int st[maxn];
int c[maxn];
char str[100];
int d[maxn][1<<8][1<<8];  

int dp(int i,int s0,int s1,int s2)
{
    if(i==M+N) return s2==(1<<S)-1?0:INF;
    int& ans=d[i][s1][s2];
    if(ans>=0) return ans;
    ans=INF;
    if(i>=M) ans=dp(i+1,s0,s1,s2);
    int m0=st[i]&s0;
    int m1=st[i]&s1;
    s0^=m0;
    s1^=m1;
    s1|=m0;
    s2|=m1;
    ans=min(ans,dp(i+1,s0,s1,s2)+c[i]);
    return ans;
}  

int main()
{
    while(scanf("%d %d %d",&S,&M,&N),S||M||N)
    {
        getchar();
        memset(d,-1,sizeof(d));
        memset(st,0,sizeof(st));
        string s;
        for(int i=0;i<M+N;i++)
        {
            cin.getline(str,100);
            s=str;
            stringstream ss(s);
            int temp;
            ss>>c[i];
            while(ss>>temp) st[i]|=1<<(temp-1);
        }
        printf("%d\n",dp(0,(1<<S)-1,0,0));
    }
    return 0;
}  

Headmaster's Headache UVa10817【DP】(缺)

原文地址:https://www.cnblogs.com/mark2017/p/9168934.html

时间: 2024-08-07 16:02:52

Headmaster's Headache UVa10817【DP】(缺)的相关文章

UVA 10817 Headmaster&#39;s Headache 状压DP

记录两个状态S1,S2分别记录哪些课程被1个人教过或2个人教过,然后记忆化搜索 UVA - 10817 Headmaster's Headache Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem D: Headmaster's Headache Time limit: 2 seconds The headmaster of Spr

uva10817 - Headmaster&#39;s Headache(01背包)

题目:uva10817 - Headmaster's Headache(01背包) 题目大意:这间学校开设S门棵,给出校长已经有的师资(n),然后再给吃m个应聘者,给出的师资和应聘者都会给出雇佣他们需要的钱还有他们会教的科目.要使得每门课都至少要有两个老师教,然后从应聘者中挑选人,要求雇佣费用最少,注意之前的请的老师也是要算进去的. 解题思路:01背包,每个应聘者要不雇用,要不就不雇佣,然后这个有8门课,用16位表示,第i门课有一个老师教的话,就在2进制的i位上放上1,如果有两个老师,就在i +

(状压dp)uva 10817 Headmaster&#39;s Headache

题目地址 1 #include <bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 const int MAX=1e5+5; 5 const int INF=1e9; 6 int s,m,n; 7 int cost[125]; 8 //char sta[MAX]; 9 string sta; 10 int able[125]; 11 int dp[125][1<<8][1<<8]; 12 in

【UVa】Headmaster&#39;s Headache(状压dp)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1758 晕....状压没考虑循环方向然后错了好久.. 这点要注意...(其实就是01背包变成了完全背包QAQ 我们将课程拆成两个点,然后状压 那么答案就是(1<<(s<<1))-1 转移就不说了,,,,,太简单.. #include <cstdio> #in

UVA - 10817 Headmaster&#39;s Headache (状压dp+记忆化搜索)

题意:有M个已聘教师,N个候选老师,S个科目,已知每个老师的雇佣费和可教科目,已聘老师必须雇佣,要求每个科目至少两个老师教的情况下,最少的雇佣费用. 分析: 1.为让雇佣费尽可能少,雇佣的老师应教他所能教的所有科目. 2.已聘老师必须选,候选老师可选可不选. 3.dfs(cur, subject1, subject2)---求出在当前已选cur个老师,有一个老师教的科目状态为 subject1,有两个及以上老师教的科目状态为 subject2的情况下,最少的雇佣费用. dp[cur][subje

UVa 10817 (状压DP + 记忆化搜索) Headmaster&#39;s Headache

题意: 一共有s(s ≤ 8)门课程,有m个在职教师,n个求职教师. 每个教师有各自的工资要求,还有他能教授的课程,可以是一门或者多门. 要求在职教师不能辞退,问如何录用应聘者,才能使得每门课只少有两个老师教而且使得总工资最少. 分析: 因为s很小,所以可以用状态压缩. dp(i, s1, s2)表示考虑了前i个人,有一个人教的课程的集合为s1,至少有两个人教的集合为s2. 在递归的过程中,还有个参数s0,表示还没有人教的科目的集合. 其中m0, m1, s0, s1, s2的计算用到位运算,还

Uva 10817 Headmaster&#39;s Headache (DP+ 状态压缩)

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

状压DP UVA 10817 Headmaster&#39;s Headache

题目传送门 1 /* 2 题意:学校有在任的老师和应聘的老师,选择一些应聘老师,使得每门科目至少两个老师教,问最少花费多少 3 状压DP:一看到数据那么小,肯定是状压了.这个状态不好想,dp[s1][s2]表示s1二进制表示下至少有1位老师的科目集合 4 s2表示至少有2位老师的科目集合所花费的最小金额,状态转移方程(01):dp[t1][t2]=min(dp[t1][t2],dp[j][k]+c[i]); 5 j,k为当前两个集合,t1,t2为转移后的集合,另外求t1,t2用到了& |位运算

UVA - 10817 Headmaster&#39;s Headache (状压类背包dp+三进制编码)

题目链接 题目大意:有S门课程,N名在职教师和M名求职者,每名在职教师或求职者都有自己能教的课程集合以及工资,要求花费尽量少的钱选择一些人,使得每门课程都有至少两人教.在职教师必须选. 可以把“每个课程已经分别有几个人教”作为状态来进行转移,每个人能教的课程集合作为“物品重量”,工资作为“价值”来更新dp值,类似01背包,每放进一个人,从后往前更新即可. 状态的表示可以用三进制编码,为了写起来舒服,我写了个结构体作为状态和编码转换的桥梁,也可以进行状态的“加法运算”,虽然速度比较慢就是了~~ 有