UVA 10817 Headmasters Headache 状压DP

记录两个状态S1,S2分别记录哪些课程被1个人教过或2个人教过,然后记忆化搜索

UVA – 10817

Headmaster’s Headache

Time Limit:3000MSMemory Limit:Unknown64bit IO Format:%lld & %llu

Submit

Description

Problem D: Headmaster’s Headache

Time limit: 2 seconds

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 integersS,MandN.S(≤ 8) is the number of subjects,M(≤ 20) is the number of serving teachers, andN(≤ 100) is the number of applicants.

Each of the followingMlines 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 toS.You must keep on employing all of them.After that there areNlines, giving the details of the applicants in the same format.

Input is terminated by a null case whereS= 0. This case should not be processed.

Output

For each test case, give the minimum cost to employ the teachers under the constraints.

Sample Input2 2 210000 120000 230000 1 240000 1 20 0 0Sample Output60000Problemsetter: Mak Yan KeiIdea from "Linear Optimization in Applications", S. L. Tang, Hong Kong University Press, 1999

Source

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) ::Volume 5. Dynamic ProgrammingRoot :: AOAPC I: Beginning Algorithm Contests — Training Guide (Rujia Liu) :: Chapter 1. Algorithm Design :: Dynamic Programming ::Exercises: BeginnerRoot :: Prominent Problemsetters ::Mak Yan KeiRoot :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 9. Dynamic Programming ::ExamplesRoot :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: More Advanced Topics :: More Advanced DP Techniques ::DP level 3Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: More Advanced Topics :: More Advanced Dynamic Programming ::DP + bitmask

Submit

/* ***********************************************Author:CKbossCreated Time :2015年02月23日 星期一 19时38分07秒File Name:UVA10817.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;const int maxn=150;int s,n,m;struct Teacher{int val,cos;}teacher[maxn];int dp[maxn][260][260];int goal;void init(){memset(teacher,0,sizeof(teacher));memset(dp,-1,sizeof(dp));goal=(1<<s)-1;}void dfs(int p,int s1,int s2){if(dp[p][s1][s2]!=-1) return ;if(s1==goal&&s2==goal) { dp[p][s1][s2]=0; return ; }if(p+1>n+m) { dp[p][s1][s2]=1e9; return ; }int np=p+1;int cost=teacher[np].val;int num=teacher[np].cos;int cf=num&s1;int ns1=s1|num;int ns2=s2|cf;dfs(np,ns1,ns2);// emplore dfs(np,s1,s2);// unemploredp[p][s1][s2]=min(dp[np][ns1][ns2]+cost,dp[np][s1][s2]);}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);while(scanf("%d%d%d",&s,&m,&n)!=EOF&&s&&n&&m){init();int sum1=0;getchar();for(int i=0;i<m+n;i++){char str[1000];gets(str);int sz=strlen(str);bool first=true;int temp=0,num=0,val=0;for(int j=0;j<sz;j++){if(str[j]>='0'&&str[j]<='9') { temp=temp*10+str[j]-'0'; }if(str[j]==' '||j==sz-1){if(first) { val=temp; first=false; }else { temp–; num|=(1<<temp); }temp=0;}}if(i<m) { sum1+=val; val=0; }teacher[i+1]=(Teacher){val,num};}dfs(0,0,0);printf("%d\n",dp[0][0][0]+sum1);}return 0;}

,要想捉大鱼,不能怕水深。要想摘玫瑰,就得不怕刺。

UVA 10817 Headmasters Headache 状压DP

相关文章:

你感兴趣的文章:

标签云: