【HDU 6006】Engineer Assignment(状压DP)
Problem Description
In Google, there are many experts of different areas. For example, MapReduce experts, Bigtable experts, SQL experts, etc. Directors need to properly assign experts to various projects in order to make the projects going smoothly.
There are N projects owned by a director. For the ith project, it needs Ci different areas of experts, ai,0,ai,1,⋅⋅⋅,ai,Ci−1 respective. There are M engineers reporting to the director. For the ith engineer, he is an expert of Di different areas, bi,0,bi,1,...,bi,Di−1.
Each engineer can only be assigned to one project and the director can assign several engineers to a project. A project can only be finished successfully if the engineers expert areas covers the project areas, which means, for each necessary area of the project, there is at least one engineer
masters it.
The director wants to know how many projects can be successfully finished.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line consisting of 2 integers, N the number of projects and M the number of engineers. Then N lines follow. The ith line containing the information of the ith project starts
with an integer Ci then Ci integers follow, ai,0,ai,1,...,ai,Ci−1 representing the expert areas needed for the ith project. Then another M lines follow. The ith line containing the information of the ith engineer starts with an integer Di then Di integers follow, bi,0,bi,1,...,bi,Di−1 representing the expert areas mastered by ith engineer.
Output
For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the maximum number of projects can be successfully finished.
limits
∙1≤T≤100.
∙1≤N,M≤10.
∙1≤Ci≤3.
∙1≤Di≤2.
∙1≤ai,j,bi,j≤100.
Sample Input
1
3 4
3 40 77 64
3 10 40 20
3 40 20 77
2 40 77
2 77 64
2 40 10
2 20 77
Sample Output
Case #1: 2
Hint
For the first test case, there are 3 projects and 4 engineers. One of the optimal solution is to assign the first(40 77) and second engineer(77 64) to project 1, which could cover the necessary areas 40, 77, 64. Assign the third(40 10) and forth(20 77) engineer to project 2, which could cover the necessary areas 10, 40, 20. There are other solutions, but none of them can finish all 3 projects.
So the answer is 2.
Source
2016 CCPC-Final
题解
对于每个项目,枚举出它对所有的工程师的选择,在枚举所有状态时,DP方程为
这是一个背包问题,每个项目有选或者不选的两种情况
参考代码
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define ll long long
#define inf 1000000000
#define PI acos(-1)
#define REP(i,x,n) for(int i=x;i<=n;i++)
#define DEP(i,n,x) for(int i=n;i>=x;i--)
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void Out(ll a){
if(a<0) putchar('-'),a=-a;
if(a>=10) Out(a/10);
putchar(a%10+'0');
}
const int N=15;
int a[N][N],b[N][N],c[N][1<<(10)+5];
ll dp[N][(1<<10)+5];
int main(){
int T=read();
REP(i,1,T){
int n=read(),m=read();
REP(i,1,n){
a[i][0]=read();
REP(j,1,a[i][0]) a[i][j]=read();
}
REP(i,1,m){
b[i][0]=read();
REP(j,1,b[i][0]) b[i][j]=read();
}
int vis[105],cnt;
mem(c,0);
REP(i,1,n){
REP(j,0,(1<<m)-1){ //枚举每个项目对所有工程师的选择情况
REP(ii,0,100) vis[ii]=0; //所有领域
cnt=0;
REP(k,1,m){
if(j&(1<<(k-1))){
cnt++;
REP(jj,1,b[k][0]) vis[b[k][jj]]=1;
}
}
if(cnt>3) continue;
int flag=0;
REP(k,1,a[i][0]) if(vis[a[i][k]]==0){
flag=1;break;
}
if(!flag) c[i][++c[i][0]]=j; //这种选择情况对i项目合法
}
}
mem(dp,0);
REP(i,1,n){
REP(j,0,(1<<m)-1){
REP(k,1,c[i][0]){
if((j|c[i][k])==j){
dp[i][j]=max(dp[i][j],dp[i-1][j-c[i][k]]+1);
}
}
dp[i][j]=max(dp[i][j],dp[i-1][j]);
}
}
printf("Case #%d: %lld\n",i,dp[n][(1<<m)-1]);
}
return 0;
}