Headmaster's Headache UVA - 10817(状压dp)
Headmaster's Headache
题意:s个科目,n个必须聘用的老师,m个可选的老师,问每个科目至少两个老师教的最少支出工资。
科目不大于8,考虑状压~
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <bits/stdc++.h> 3 using namespace std; 4 const int INF= 0x3f3f3f3f; 5 const int maxn=125; 6 const int maxx=8; 7 int m,n,s; 8 int c[maxn],st[maxn],d[maxn][1<<maxx][1<<maxx]; 9 10 int dp(int i,int s0,int s1,int s2) 11 { 12 if(i==m+n) return s2==(1<<s)-1 ? 0 : INF; 13 int &ans=d[i][s1][s2]; 14 if(ans>=0) return ans; 15 ans=INF; 16 if(i>=m) ans=dp(i+1,s0,s1,s2); //不选 17 int m0=st[i]&s0; 18 int m1=st[i]&s1; 19 s0^=m0; 20 s1=(s1^m1)|m0; 21 s2|=m1; 22 ans=min(ans,c[i]+dp(i+1,s0,s1,s2)); //选 23 return ans; 24 } 25 int main() 26 { 27 while(scanf("%d%d%d",&s,&m,&n)&&(s||m||n)) 28 { 29 memset(d,-1,sizeof(d)); 30 getchar(); 31 for(int i=0;i<m+n;i++) 32 { 33 st[i]=0; 34 string line; 35 int x; 36 getline(cin,line); 37 stringstream ss(line); 38 ss>>c[i]; 39 while(ss>>x) 40 { 41 x--; 42 st[i]|=(1<<x); 43 } 44 // cout<<"能教的科目为: "; 45 //for(int j=0;j<s;j++) if(st[i]&(1<<j)) cout<<j+1<<' '; 46 //cout<<endl; 47 } 48 printf("%d\n",dp(0,(1<<s)-1,0,0)); 49 } 50 return 0; 51 52 }