UVa 10817 - Headmaster's Headache ( 状态压缩dp)
2015-05-08 20:31
435 查看
Description
Problem D: Headmaster's Headache |
Time limit: 2 seconds |
wants to select applicants so that each subject is taught by at least two teachers, and the overall cost is minimized.
InputThe input consists of several test cases. The format of each of them is explained below:
The first line contains three positive integers [b]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 toS. You must keep on employing all of them. After that there areN 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
[/b]
题意:
某校有n个教师和m个求职者,已知每人的工资和能教的课程集合,要求支付最少的工资使得每门课都至少有两名教师教学。在职教师必须招聘。思路:
dp[s1][s2]: s1表示课程集合{ s1 }都至少有一个教师教的情况。s2表示课程集合{ s2 }都至少有两个教师教的情况。
每个求职者的pi, 对于每个求职者,要么选,要么不选,就是01背包问题。
对于s1,s2,可以根据当前枚举到的求职者课程即可,可推出下一个状态:
nextS1 = p[i] | s1,
nextS2 = (p[i] & s1) | s2
dp[nextS1][nextS2] = min(dp[nextS1][nextS2],dp[s1][s2] + p[i])
<span style="font-size:18px;"> #include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <string> #include <algorithm> #include <queue> #include <stack> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 150; const double PI = acos(-1.0); const double e = 2.718281828459; const double eps = 1e-8; int dp[1<<10][1<<10]; int p[MAXN], c[MAXN]; int cnt[MAXN]; char s[1010]; int n, m, course; int st1, st2; int sum, maxState; int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); //cout<<INF<<endl; while(cin>>course>>m>>n) { if(!course) break; sum = 0; st1 = 0; st2 = 0; memset(cnt, 0, sizeof(cnt)); memset(p, 0, sizeof(p)); memset(c, 0, sizeof(c)); for(int i = 1; i <= n+m; i++) { scanf("%d", &c[i]); gets(s); //puts(s); for(int j = 0; s[j]; j++) { if(s[j] != ' ') { int t = s[j]-'0'-1; p[i] |= (1<<t); if(i <= m) cnt[t]++; } } if(i <= m) { sum += c[i]; st1 |= p[i]; } } for(int i = 0; i < course; i++) { if(cnt[i] >= 2) st2 |= (1<<i); } memset(dp, INF, sizeof(dp)); //printf("%d %d %d %d\n", dp[0][0], dp[0][1], dp[0][2], dp[1][1]); dp[st1][st2] = sum; //printf("%d %d %d %d\n", st1, st2, sum, (1<<course)-1); maxState = (1<<course)-1; for(int i = m+1; i <= n+m; i++) { for(int s1 = maxState; s1 >= 0; s1--) { for(int s2 = maxState; s2 >= 0; s2--) { if(dp[s1][s2] == INF) continue; int nextS1 = s1|p[i]; int nextS2 = (s1&p[i])|s2; dp[nextS1][nextS2] = min(dp[nextS1][nextS2], dp[s1][s2]+c[i]); //printf("%d %d %d\n", nextS1, nextS2, dp[nextS1][nextS2]); } } } printf("%d\n", dp[maxState][maxState]); } return 0; }</span>
本文出自
/article/1350093.html
相关文章推荐
- UVa 10817 - Headmaster's Headache (简单DP 状态压缩)
- UVA 10817 Headmaster's Headache(dp 状态压缩 01背包)
- Uva 10817 Headmaster's Headache (DP+ 状态压缩)
- Uva 10817 - Headmaster's Headache(状态压缩DP)
- uva 10817 - Headmaster's Headache ( 状态压缩dp)
- uva 10817 - Headmaster's Headache ( 状态压缩dp)
- UVA10817-----Headmaster's Headache-----状态压缩的背包(记忆化搜索实现)
- uva 10817 Headmaster's Headache(DP 3进制压缩,4级)
- UVA - 10817 Headmaster's Headache 状态压缩
- UVA10817 Headmaster's Headache 状态压缩的01背包
- UVA 10817——Headmaster's Headache(DP)
- UVa 10817 Headmaster's Headache (状压DP)
- UVA 10817 Headmaster's Headache(状压DP)
- Headmaster's Headache - UVa 10817 状压dp
- uva 10817 Headmaster's Headache 出发dp 位计算
- UVA 10817-Headmaster’s Headache(状态压缩DP)
- UVA 10817 - Headmaster's Headache(位运算模拟子集+dp)
- Headmaster's Headache UVA - 10817 (状态压缩dp+01背包)
- Uva-10817 Headmaster's Headache
- UVA 10817 Headmaster's Headache