UVA 10817 校长的烦恼(状压DP+记忆化搜索

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 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
2 2 2 10000 1 20000 2 30000 1 2 40000 1 2 0 0 0
Sample Output
60000

#include<iostream>
#include<fstream>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<queue>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll unsigned long long
#define MAX 1000000000
#define ms memset
#define maxn 60005

#define INF 1000000

#define debug puts("YES");
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;

int n,m,k;
/*
题目大意:有m个教师和k个候选,给定对应的工资和对应教授的课程,
要求使得课程每个都有至少两个人教的方案其对应的最小的代价是多少。

状态压缩,思路是抄袭紫书上的(惭愧),
紫书上的确实精华,状态转移限制在三种状态之间,
s0:没有人教的课程集合,s1:恰好有一个人教的课程集合,s2:大于等于2的人数课程教的即可。

那么对于给定的当前的职员的状态sta[i],有关于位运算的状态转移。
初始状态是s1和s2都为零,s0是全选的状态。

*/
int cost[130];
int sta[130];///
char c;

int d[130][1<<9][1<<9];
int dp(int i,int s0,int s1,int s2)///三种状态
{
    if(i==m+k) return s2==(1<<n)-1?0:INF;
    int &ans=d[i][s1][s2];
    if(ans>=0) return ans;
    ans=INF;
    if(i>=m) ans=min(ans,dp(i+1,s0,s1,s2));
    int m0=s0&sta[i],m1=s1&sta[i];
    s0^=m0,s1=(s1^m1)|m0,s2|=m1;
    ans=min(ans,cost[i]+dp(i+1,s0,s1,s2));
    return ans;
}

int main()
{
    while(scanf("%d%d%d",&n,&m,&k)&&(n||m||k))
    {
        for(int i=0;i<m+k;i++)
        {
            scanf("%d",&cost[i]);
            getchar();int st=0;
            while((c=getchar())!='\n')   if(c<='8'&&c>='1') st+=1<<(c-'1');
            sta[i]=st;
        }
        memset(d,-1,sizeof(d));
       printf("%d\n", dp(0,(1<<n)-1,0,0));///记忆化搜索
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值