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.
大致题意:有n个任务,m个工程师,完成一个任务需要一些领域,每个工程师会一些领域,问最多能完成多少任务
思路:状压dp
代码如下
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int dp[15][1<<11];
int a[15][5];
int b[15][5];
int vis[110];
vector<int> ch[15];
int n,m;
int main()
{
int T;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
int k;
scanf("%d",&k);
a[i][0]=k;
for(int j=1;j<=k;j++)
scanf("%d",&a[i][j]);
}
for(int i=0;i<m;i++)
{
int k;
scanf("%d",&k);
b[i][0]=k;
for(int j=1;j<=k;j++)
scanf("%d",&b[i][j]);
}
memset(dp,0,sizeof(dp));
for(int i=0;i<15;i++)
ch[i].clear();
for(int i=1;i<=n;i++)
{
for(int state=0;state<(1<<m);state++)
{
memset(vis,0,sizeof(vis));
int cnt=0;
for(int j=0;j<m;j++)
{
if(state&(1<<j))//状态state中选了j号工程师
{
cnt++;
for(int k=1;k<=b[j][0];k++)
vis[b[j][k]]=1;
}
}
if(cnt>3) continue; //最多选3个工程师,选多了没用
int flag=1;
for(int j=1;j<=a[i][0];j++)
if(vis[a[i][j]]==0)
{
flag=0;
break;
}
if(flag)
ch[i].push_back(state);
}
}
for(int i=1;i<=n;i++)
{
for(int state=0;state<(1<<m);state++)
{
int number=ch[i].size();
for(int j=0;j<number;j++)
if((state|ch[i][j])==state)//此时的状态满足任务i的需求
dp[i][state]=max(dp[i-1][state-ch[i][j]]+1,dp[i][state]);//选这个状态
dp[i][state]=max(dp[i][state],dp[i-1][state]);//选还是不选这个状态
}
}
printf("Case #%d: %d\n",cas,dp[n][(1<<m)-1]);
}
return 0;
}