您的位置:首页 > 其它

uva 10817 - Headmaster's Headache

2014-08-11 01:43 387 查看
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 andNS (≤ 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背包,用第一维表示第二节课是否有老师上,第二维表示第一节课是否有老师上。

之后就是位运算进行转移了,其余和二维背包一模一样,逆向递推减少一维空间。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#define Maxn 130
using namespace std;

int v[Maxn],w[Maxn];
char str[Maxn];
int dp[260][260];
const int inf=0x3f3f3f3f;
int main()
{
int n,m,s;
while(scanf("%d%d%d",&s,&m,&n),s){
memset(w,0,sizeof w);
for(int i=0;i<m+n;i++){
scanf("%d",v+i);
gets(str);
int id=0;
while(str[id]){
if(str[id]!=' ') w[i]|=1<<str[id]-'0'-1;
id++;
}
}
int s1=0,s2=0,money=0;
for(int i=0;i<m;i++){
s2|=s1&w[i];
s1|=w[i];
money+=v[i];
}
memset(dp,0x3f,sizeof dp);
dp[s2][s1]=money;
int maxsize=(1<<s)-1;
for(int i=m;i<n+m;i++)
for(int j=maxsize;j>=0;j--)
for(int k=maxsize;k>=0;k--)
if(dp[j][k]!=inf){
int ns2=k&w[i]|j,ns1=w[i]|k;
dp[ns2][ns1]=min(dp[ns2][ns1],dp[j][k]+v[i]);
}
printf("%d\n",dp[maxsize][maxsize]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: