记录两个状态S1,S2分别记录哪些课程被1个人教过或2个人教过,然后记忆化搜索
UVA - 10817
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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.
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.
For each test case, give the minimum cost to employ the teachers under the constraints.
2 2 2 10000 1 20000 2 30000 1 2 40000 1 2 0 0 0
60000Problemsetter: Mak Yan Kei
Source
/* *********************************************** Author :CKboss Created 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);// unemplore dp[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; }