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.
The first line contains three positive integers S, M andN. 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.
这道题是二维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;
}
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 S, M andN. 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
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;
}
相关文章推荐
- uva 10817 Headmaster's Headache
- uva 10817 - Headmaster's Headache 校长的烦恼
- UVA 10817 Headmaster's Headache
- uva 10817 - Headmaster's Headache(01背包)
- UVa 10817 - Headmaster's Headache (简单DP 状态压缩)
- UVA-10817 Headmaster's Headache
- uva 10817 - Headmaster's Headache ( 状态压缩dp)
- UVA 10817 Headmaster's Headache(01背包+状压DP)
- UVA 10817 Headmaster's Headache 状压DP
- Headmaster's Headache - UVa 10817 状压dp
- UVA - 10817 Headmaster's Headache 状态压缩
- UVA 10817 Headmaster's Headache
- UVA 10817 Headmaster's Headache
- Headmaster's Headache UVA - 10817 状态压缩
- UVA 10817 Headmaster's Headache(状压DP)
- UVa 10817 Headmaster's Headache
- uva 10817 - Headmaster's Headache ( 状态压缩dp)
- Headmaster's Headache UVA - 10817 (状态压缩dp+01背包)
- UVA 10817 Headmaster’s Headache
- UVA 10817 Headmaster's Headache(状压dp)