Uva--10817(动规,状态压缩,01背包)

本文介绍了一种解决教师招聘问题的算法,旨在确保每门课程至少由两位老师教授的同时,使得总招聘成本最低。利用状态压缩和01背包的概念,通过记忆化搜索实现了最优解的求解。

2014-08-25 09:43:17

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

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

Sample Output

60000

思路:这题让人自然地想到两个东西:状态压缩和01背包。由于最多只有8门课,每门课至少两个老师教,所以每门课占两个二进制位(如:第 i 门课有一个老师教,1 << (i - 1) * 2为1,若有两个老师教的话,1 << (i - 1) * 2、1 << (i - 1) * 2 + 1,两个位置都为1。),对于每个应聘的老师,取或不去,是01背包问题。
  
鉴于此,一开始用递推怎么都WA,原因在于:对于一个状态,你难以确定其前驱状态,原因在于对于当前状态 s,考虑的是第 i 个老师,若第 j 门课已经至少有2名老师在教,那么如果第 i 个老师也能教第 j 门课的话,那么前驱状态就有两种:(1)第 j 门课是在第 i 个老师加入后才满足至少2名老师的要求。(2)第 j 门课在第 i 个老师加入之前就已经达到要求。
  因此单纯地用递推难以实现,所以这里用了记忆化搜索来做。dp[i][j]表示前 i 个老师达到状态 j 的最小薪水和。
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <string>
 6 #include <sstream>
 7 #include <iostream>
 8 #include <algorithm>
 9 using namespace std;
10 typedef long long ll;
11 const int INF = 1 << 30;
12 
13 struct teacher{
14     int c,s;
15 }t[105];
16 
17 int dp[105][1 << 16];
18 int S,M,N,sum,s;
19 int tar;
20 
21 int Solve(int x,int y){
22     int &res = dp[x][y];
23     if(res != -1)
24         return res;
25     if(y == tar)
26         return 0;
27     if(x >= N)
28         return INF;
29     int ty = y;
30     int f1,f2;
31     for(int i = 0; i < S; ++i){
32         if((t[x].s & (1 << i)) == 0)
33             continue;
34         f1 = (ty & (1 << (i * 2)));
35         f2 = (ty & (1 << (i * 2 + 1)));
36         if(f1 && f2)
37             continue;
38         if(f1 == 0){
39             ty |= (1 << (i * 2));
40         }
41         else{
42             ty |= (1 << (i * 2 + 1));
43         }
44     }
45     res = INF;
46     return res = min(res,min(Solve(x + 1,y),Solve(x + 1,ty) + t[x].c));
47 }
48 
49 int main(){
50     while(scanf("%d%d%d",&S,&M,&N) == 3 && S){
51         getchar();
52         memset(t,0,sizeof(t));
53         s = sum = 0;
54         int tc,ts;
55         string str;
56         for(int i = 0; i < M; ++i){
57             getline(cin,str);
58             istringstream sin(str);
59             sin >> tc;
60             sum += tc;
61             while(sin >> ts){
62                 if((s & (1 << ((ts - 1) * 2))) == 0)
63                     s ^= 1 << ((ts - 1) * 2);
64                 else if((s & (1 << ((ts - 1) * 2 + 1))) == 0){
65                     s ^= 1 << ((ts - 1) * 2 + 1);
66                 }
67             }
68         }
69         for(int i = 0; i < N; ++i){
70             getline(cin,str);
71             istringstream sin(str);
72             sin >> t[i].c;
73             while(sin >> ts)
74                 t[i].s ^= 1 << (ts - 1);
75         }
76         tar = (1 << 2 * S) - 1;
77         memset(dp,-1,sizeof(dp));
78         printf("%d\n",Solve(0,s) + sum);
79     }
80     return 0;
81 }

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3934338.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值