您的位置:首页 > 其它

UVa 10817 - Headmaster's Headache ( 状态压缩dp)

2015-05-08 20:31 435 查看



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 [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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: