UVA 10817 Headmaster's Headache(01背包+状压DP)

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 SM and NS (≤ 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 toSYou 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
 

【思路】

首先老师有选与不选两种可能,所以这是一道01背包问题,由于选择老师之后课程对于老师的需求改变了,所以要记录课程状态,S值仅为8,因此要进行状态压缩。用二进制串s1、s2分别表示至少有一个、至少有两个老师的课程状态。dp[s1][s2]记录下s1和s2状态下的老师薪资最小值,转移的时候要由高到低。一个比较麻烦的是读入,用stringstream来做比较省事,流对象的构造和析构比较费时,所以干脆只用一个对象,多次利用,每次用完都清空就好了。

【代码】

#include<iostream>
#include<cstring>
#include<algorithm>
#include<sstream>
using namespace std;

const int MAXN=130,MAXS=300,INF=0x3f3f3f3f;

int s,m,n;
int c[MAXN],p[MAXN];
int dp[MAXS][MAXS];

int main()
{
    string line;
    stringstream stream;
    while(getline(cin,line)){
        stream<<line;
        stream>>s>>m>>n;
        stream.str("");stream.clear();
        if(s==0)break;
        int curr1=0,curr2=0,sum=0;
        for(int i=1;i<=m;i++){
            getline(cin,line);
            stream<<line;
            int x,course=0;
            stream>>x;
            sum+=x;
            while(stream>>x)course|=(1<<(x-1));
            stream.str("");stream.clear();
            curr2|=(curr1&course);
            curr1|=course;
        }
        memset(c,0,sizeof(c));
        memset(dp,0x3f,sizeof(dp));
        dp[curr1][curr2]=sum;
        for(int i=1;i<=n;i++){
            getline(cin,line);
            stream<<line;
            stream>>p[i];
            int x;
            while(stream>>x)c[i]|=(1<<(x-1));
            stream.str("");stream.clear();
            for(int j=(1<<s)-1;j>=0;j--)
                for(int k=(1<<s)-1;k>=j;k--){
                    if(dp[k][j]>=INF)continue;
                    int s1=k|c[i],s2=(k&c[i])|j;
                    dp[s1][s2]=min(dp[s1][s2],dp[k][j]+p[i]);
                }
        }
        cout<<dp[(1<<s)-1][(1<<s)-1]<<endl;
    }
    return 0;
}


【资源介绍】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,也可以作为小白实战演练和初期项目立项演示的重要参考借鉴资料。 3、本资源作为“学习资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研和多多调试实践。 图像数据处理工具+数据(帮助用户快速划分数据集并增强图像数据集。通过自动化数据处理流程,简化了深度学习项目的数据准备工作).zip 图像数据处理工具+数据(帮助用户快速划分数据集并增强图像数据集。通过自动化数据处理流程,简化了深度学习项目的数据准备工作).zip 图像数据处理工具+数据(帮助用户快速划分数据集并增强图像数据集。通过自动化数据处理流程,简化了深度学习项目的数据准备工作).zip 图像数据处理工具+数据(帮助用户快速划分数据集并增强图像数据集。通过自动化数据处理流程,简化了深度学习项目的数据准备工作).zip 图像数据处理工具+数据(帮助用户快速划分数据集并增强图像数据集。通过自动化数据处理流程,简化了深度学习项目的数据准备工作).zip 图像数据处理工具+数据(帮助用户快速划分数据集并增强图像数据集。通过自动化数据处理流程,简化了深度学习项目的数据准备工作).zip 图像数据处理工具+数据(帮助用户快速划分数据集并增强图像数据集。通过自动化数据处理流程,简化了深度学习项目的数据准备工作).zip 图像数据处理工具+数据(帮助用户快速划分数据集并增强图像数据集。通过自动化数据处理流程,简化了深度学习项目的数据准备工作).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值