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
思路:3进制状态压缩。
#include<iostream> #include<cstring> #include<cstdio> #define FOR(i,a,b) for(int i=a;i<=b;++i) #define clr(f,z) memset(f,z,sizeof(f)) using namespace std; const int mm=110; int S,M,N; int dp[mm][10000],ke[9],w[mm],te[mm][9],p[9],P; char s[100000]; bool getdata() { scanf("%d%d%d",&S,&M,&N);gets(s); if(S||M||N)return 1; return 0; } void solve() { int len,pos,x,COST=0; FOR(i,1,S)ke[i]=2; clr(te,0); FOR(i,1,M) { scanf("%d",&x); gets(s); //cout<<s<<endl; COST+=x; //pos=0;//while(s[pos]!=' ')++pos; for(int j=0;s[j];++j) if(isdigit(s[j])) {ke[s[j]-'0']--; } } FOR(i,1,N) { scanf("%d",&w[i]); gets(s);//cout<<s<<endl; for(int j=0;s[j];++j) if(isdigit(s[j])) { te[i][ s[j]-'0' ]++; } } clr(dp,0x3f); P=0; FOR(i,1,S) P=P*3+2;int tmp; FOR(i,0,P) {tmp=i; FOR(j,1,S) { p[j]=tmp%3;tmp/=3; } int j; for(j=1;j<=S&&p[j]>=ke[j];++j); if(j==S+1) dp[0][i]=COST;///缺i状态 } FOR(i,1,N)FOR(j,0,P) { dp[i][j]=dp[i-1][j]; tmp=j; FOR(j,1,S) { p[j]=tmp%3;tmp/=3; } FOR(j,1,S) if(te[i][j]&&p[j]<2) { p[j]++; } tmp=0; for(int j=S;j>=1;--j) tmp=tmp*3+p[j]; if(dp[i][j]>dp[i-1][tmp]+w[i]) dp[i][j]=dp[i-1][tmp]+w[i]; } printf("%d\n",dp[N][0]); } int main() { //freopen("data.in","r",stdin); while(getdata()) { solve(); } }