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
#include
#include
#include//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include
#include
#include
#include
#include
#include
#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<=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='1') st+=1<<(c-'1');
            sta[i]=st;
        }
        memset(d,-1,sizeof(d));
       printf("%d\n", dp(0,(1<

 

你可能感兴趣的:(UVA习题集,动态规划之状压DP,搜索(DFS+BFS))