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

本博客讨论了学校在考虑雇佣新教师时如何通过动态规划解决成本最小化的问题,确保每门课程至少由两位教师教授。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Download as PDF

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 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

题意:

某校有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>



本文出自   http://blog.youkuaiyun.com/shuangde800/article/details/9776645

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值