记录两个状态S1,S2分别记录哪些课程被1个人教过或2个人教过,然后记忆化搜索
UVA - 10817
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
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 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
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 -- 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
/* ***********************************************
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;
}