UVA 10817 Headmaster'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 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 and NS (≤ 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

Problemsetter: Mak Yan Kei

Idea from "Linear Optimization in Applications", S. L. Tang, Hong Kong University Press, 1999

Source

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 5. Dynamic Programming

Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 1. Algorithm Design :: Dynamic Programming :: Exercises:
Beginner

Root :: Prominent Problemsetters :: Mak Yan Kei

Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 9. Dynamic Programming :: Examples

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: More Advanced Topics :: More Advanced DP Techniques :: DP
level 3

Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: More Advanced Topics :: More Advanced Dynamic Programming :: DP
+ bitmask

Submit Status

/* ***********************************************
Author        :CKboss
Created Time  :2015年02月23日 星期一 19时38分07秒
File Name     :UVA10817.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int maxn=150;

int s,n,m;

struct Teacher
{
	int val,cos;
}teacher[maxn];

int dp[maxn][260][260];
int goal;

void init()
{
	memset(teacher,0,sizeof(teacher));
	memset(dp,-1,sizeof(dp));
	goal=(1<<s)-1;
}

void dfs(int p,int s1,int s2)
{
	if(dp[p][s1][s2]!=-1) return ;
	if(s1==goal&&s2==goal) { dp[p][s1][s2]=0; return ; }
	if(p+1>n+m) { dp[p][s1][s2]=1e9; return ; }

	int np=p+1;
	int cost=teacher[np].val;
	int num=teacher[np].cos;
	int cf=num&s1;
	int ns1=s1|num;
	int ns2=s2|cf;
	dfs(np,ns1,ns2);// emplore
	dfs(np,s1,s2);// unemplore
	dp[p][s1][s2]=min(dp[np][ns1][ns2]+cost,dp[np][s1][s2]);
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

	while(scanf("%d%d%d",&s,&m,&n)!=EOF&&s&&n&&m)
	{
		init();
		int sum1=0;
		getchar();
		for(int i=0;i<m+n;i++)
		{
			char str[1000];
			gets(str);
			int sz=strlen(str);
			bool first=true;
			int temp=0,num=0,val=0;
			for(int j=0;j<sz;j++)
			{
				if(str[j]>='0'&&str[j]<='9') { temp=temp*10+str[j]-'0'; }
				if(str[j]==' '||j==sz-1)
				{
					if(first) { val=temp; first=false; }
					else { temp--; num|=(1<<temp); }
					temp=0;
				}
			}
			if(i<m) { sum1+=val; val=0; }
			teacher[i+1]=(Teacher){val,num};
		}
		dfs(0,0,0);
		printf("%d\n",dp[0][0][0]+sum1);
	}

    return 0;
}

UVA 10817 Headmaster's Headache 状压DP

时间: 2024-12-31 10:01:52

UVA 10817 Headmaster's Headache 状压DP的相关文章

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

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

(状压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 - 10817 Headmaster&#39;s Headache (状压dp+记忆化搜索)

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

状压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+ 状态压缩)

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

UVA 10817 - Headmaster&#39;s Headache

题目:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&page=show_problem&problem=1758 状态压缩的DP,dp[i][st]表示状态为st考虑后面i个人所有人最小花费, 因为每个科目有三种状态,可以用一个三进制数表示, 状态不是很多,所以可以把预先把每个数的三进制预处理出来, 决策为选和不选. #include<bits/std

uva 11825 Hackers&#39; Crackdown (状压dp,子集枚举)

题目链接:uva 11825 题意: 你是一个黑客,侵入了n台计算机(每台计算机有相同的n种服务),对每台计算机,你可以选择终止一项服务,则他与其相邻的这项服务都终止.你的目标是让更多的服务瘫痪(没有计算机有该项服务). 思路:(见大白70页,我的方程与大白不同) 把n个集合P1.P2.Pn分成尽量多的组,使得每组中所有集合的并集等于全集,这里的集合Pi是计算机i及其相邻计算机的集合,用cover[i]表示若干Pi的集合S中所有集合的并集,dp[s]表示子集s最多可以分成多少组,则 如果cove

UVa 1252 - Twenty Questions(状压DP)

链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3693 题意: 有n(n≤128)个物体,m(m≤11)个特征.每个物体用一个m位01串表示,表示每个特征是具备还是不具备.我在心里想一个物体(一定是这n个物体之一),由你来猜.你每次可以询问一个特征,然后我会告诉你:我心里的物体是否具备这个特征.当你确定答案之后,就把答案告诉我(告

UVa 1412 - Fund Management(状压DP + 预处理)

链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4158 题意: 你有c(0.01≤c≤1e8)美元现金,但没有股票.给你m(1≤m≤100)天时间和n(1≤n≤8)支股票供你买卖,要求最后一天结束后不持有任何股票,且剩余的钱最多.买股票不能赊账,只能用现金买.已知每只股票每天的价格(0.01-999.99.单位是美元/股)与参数s