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

猜你喜欢

转载自blog.csdn.net/Mannix_Y/article/details/85061548