题目:
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