Headmaster's Headache UVA - 10817 (01背包状压dp)

 

The headmaster of Spring Field School is consider- ing employing some new teachers for certain subjects. There are a number of teach- ers applying for the posts. Each teacher is able to teach one or more sub jects. The headmaster wants to select applicants so that each sub- ject 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

222 10000 1 20000 2 30000 1 2 40000 1 2 000

Sample Output

60000

题意:有s个学科,现在在学校有n个教师在教书,这些教师必须要被雇佣,现在还有m个教师正在应聘。现在给出这n个在职教师的工资和能教的科目,给出m个应聘教师的工资和能教的科目,现在希望这s个科目,每个都有至少两个教师教授,问你最少需要支付的工资是多少。(不能解雇原来的老师)

题解:简单状压的01背包问题,看代码和注释

//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<sstream>
#include<iterator>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node << 1
#define rson mid + 1, r, node << 1 | 1
const int INF  =  0x3f3f3f3f;
const int O    =  1e5;
const int mod  =  1e4 + 7;
const int maxn =  1e2+5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;


int s, m, n, sum, sta1, sta2;
int cost[maxn], sub[maxn];
int dp[2][1<<8][1<<8];
// sum为所有老师的费用和,sta1,sta2分别表示所有老师总教学状态,分别对应下面的s1和s2
// cost[i],sub[i]分别表示第i为应聘者的费用和教学状态
// s1表示恰有一人教的科目集合,s2表示已经至少两人教的科目集合
// dp[i][s1][s2]表示前i个应聘者达到状态s1和s2的最小花费(这里i只开了2维,可滚动一下数组,节约内存)
// 运用01背包的思想编码

void init(){
    sum = 0; sta1 = 0; sta2 = 0;
    MT(cost, 0); MT(sub, 0);
    int fee; char c;
    while(m --) {
        scanf("%d", &fee); sum += fee;
        while((c = getchar()) != '\n' ){
            if(c == ' ') continue;
            int s1 = sta1, s2 = sta2;
            int num = 1 << (c - '1');
            sta1 = ((s2 ^ num) & num) ^ s1;
            sta2 = (s1 & num) | s2;
        }
    }
    for(int i=1; i<=n; i++) {
        scanf("%d", &fee); cost[i] = fee;
        while((c = getchar()) != '\n'){
            if(c == ' ') continue;
            sub[i] ^= 1 << (c - '1');
        }
    }
}

int main(){
    while(scanf("%d%d%d", &s, &m, &n) && s+n+m){
        getchar();
        init(); //读入数据并计算sta1,sta2和sum
        MT(dp, INF); dp[0][sta1][sta2] = sum;
        for(int i=1; i<=n; i++) {
            for(int s1=0; s1<(1<<s); s1++){
                for(int s2=0; s2<(1<<s); s2++){
                    if(s2 & s1) continue;
                    int ss1 = ((s2 ^ sub[i]) & sub[i]) ^ s1;
                    int ss2 = (s1 & sub[i]) | s2;
                    // ss1, ss2 分别表示在s1,s2状态下加入第i为应聘者的状态
                    dp[i&1][ss1][ss2] = min(dp[!(i&1)][s1][s2]+cost[i], dp[i&1][ss1][ss2]);
                    dp[i&1][ss1][ss2] = min(dp[!(i&1)][ss1][ss2], dp[i&1][ss1][ss2]);
                    dp[i&1][s1][s2] = min(dp[i&1][s1][s2], dp[!(i&1)][s1][s2]);
                }
            }
        }
        printf("%d\n", dp[n&1][0][(1<<s)-1]);
    }
    return 0;
}

附几组数据


2 2 2
10000 1
20000 2
30000 1 2
40000 1 2

3 2 2
100 1 2
100
100 3
100 1 2 3

4 3 3
1 1 2 3
10 1
10 2
1000 3
100 3 4
100 4

2 2 2
100 1 2
100 1
200
300 2

3 2 2
100 1 2
100 1
1000 2 3
100 1 2 3

3 3 3
1 1 2
1 1 2
1 1 2
100 1 3
1 3
2 1 2 3

4 4 4
213 1 2 3
2313 1 2 4
3213 1 2
3213 1
123 1 2 3
3214 1 2 4
3123 1 2 4
313 1 2 3

答案:
60000
400
221
500
1300
6
12198

 

创建校长信息表`HeadMaster`的SQL语句如下: ``` CREATE TABLE HeadMaster ( HeadID CHAR(4) NOT NULL, Head_Name CHAR(10) NOT NULL, S_ID CHAR(4) NOT NULL, App_Date DATETIME, CONSTRAINT PK_HeadMaster PRIMARY KEY (HeadID), CONSTRAINT FK_HeadMaster_S_ID FOREIGN KEY (S_ID) REFERENCES School(S_ID) ); ``` 其中,`HeadID`、`Head_Name`、`S_ID`、`App_Date`分别是校长信息表`HeadMaster`的列名,对应的中文列名分别是校长编号、姓名、学校编号、任职日期。数据类型和长度分别如题所述。`App_Date`列为校长的任职日期,使用了`Datetime`类型。 主键约束使用`PK_HeadMaster`名称进行命名,外键约束使用`FK_HeadMaster_S_ID`进行命名,对应`S_ID`列的外键关系。约束必须引用`School`表的`S_ID`列。 以上SQL语句创建了一个校长信息表`HeadMaster`,其中主键为`HeadID`列,`S_ID`列为`School`表的外键。 创建学校信息表`School`的SQL语句如下: ``` CREATE TABLE School ( S_ID CHAR(4) NOT NULL, S_Name VARCHAR(20) NOT NULL, s_add CHAR(20), CONSTRAINT PK_School PRIMARY KEY (S_ID) ); ``` 其中,`S_ID`、`S_Name`、`s_add`分别是学校信息表`School`的列名,对应的中文列名分别是学校编号、学校名称、学校地址。数据类型和长度分别如题所述。 主键约束使用`PK_School`名称进行命名。 以上SQL语句创建了一个学校信息表`School`,其中主键为`S_ID`列。 创建教师类型表`TeacherType`的SQL语句如下: ``` CREATE TABLE TeacherType ( TypeID CHAR(4) NOT NULL, TypeName CHAR(20) NOT NULL, CONSTRAINT PK_TeacherType PRIMARY KEY (TypeID) ); ``` 其中,`TypeID`、`TypeName`分别是教师类型表`TeacherType`的列名,对应的中文列名分别是类型编号、类型名称。数据类型和长度分别如题所述。 主键约束使用`PK_TeacherType`名称进行命名。 以上SQL语句创建了一个教师类型表`TeacherType`,其中主键为`TypeID`列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值