hdu6006(状压dp)

来源:互联网 发布:万界淘宝商 编辑:程序博客网 时间:2024/06/06 06:59

Engineer Assignment

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 781    Accepted Submission(s): 269


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,Ci1 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,Di1.
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,Ci1 representing the expert areas needed for the ith project. Then another M lines follow. The ithline containing the information of the ith engineer starts with an integer Di then Di integers follow, bi,0,bi,1,...,bi,Di1 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


1T100.
1N,M10.
1Ci3.
1Di2.
1ai,j,bi,j100.
 

Sample Input
13 43 40 77 643 10 40 203 40 20 772 40 772 77 642 40 102 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个工程师,每个掌握几个领域。求能完成的工程数的最大值。

由于n、m很小,选择状态压缩的方法,看了别人代码才敲出来的。


#include<iostream>#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>using namespace std;int proj[15][10];               //工程需要领域知识  proj[i][0]表示个数 int eng[15][10];               //工程师掌握领域    eng[i][0]表示个数 int dp[15][1<<11];             //dp[i][st]表示前i个工程在使用工程师的状态为st的情况下能完成的最多工程数 vector<int>engstate[15];       //存储每个工程需要工程师方案的可能状态 bool area[105];                //表示领域覆盖状况 int main(){int t;int n,m;scanf("%d",&t);for(int tt=1;tt<=t;tt++){memset(proj,0,sizeof(proj));memset(eng,0,sizeof(eng));memset(dp,0,sizeof(dp));for(int i=0;i<15;i++)engstate[i].clear();scanf("%d%d",&n,&m);for(int i=0;i<n;i++)    //输入工程所需领域 {scanf("%d",&proj[i][0]);for(int j=1;j<=proj[i][0];j++){scanf("%d",&proj[i][j]);}}for(int i=0;i<m;i++)      //输入工程师掌握领域 {scanf("%d",&eng[i][0]);for(int j=1;j<=eng[i][0];j++){scanf("%d",&eng[i][j]);}}for(int i=0;i<n;i++)        //求每个工程需要工程师方案的可能状态 {for(int st=0;st<(1<<m);st++){memset(area,0,sizeof(area));int cnt=0;for(int k=0;k<m;k++){if(st&(1<<k))     //找出这种状态下的工程师,求出这种状态下掌握领域状态 {cnt++;for(int k1=1;k1<=eng[k][0];k1++){area[eng[k][k1]]=1;}}}if(cnt>3)continue;      //多余,剪枝 bool flag=true;for(int k=1;k<=proj[i][0];k++){if(!area[proj[i][k]])       //对于这个工程,有没有覆盖到的领域 {flag=false;}}if(flag)    //符合要求 {engstate[i].push_back(st);}}}for(int i=0;i<n;i++){for(int st=0;st<(1<<m);st++){for(int j=0;j<engstate[i].size();j++){if((st|engstate[i][j])==st)   //可以完成工程i {if(i>0)dp[i][st]=max(dp[i][st],dp[i-1][st-engstate[i][j]]+1);     //选择前i个能完成工程i时最多完成工程数 else dp[i][st]=1;}}if(i>0)   //比较选与不选择完成工程i dp[i][st]=max(dp[i][st],dp[i-1][st]);}}printf("Case #%d: %d\n",tt,dp[n-1][(1<<m)-1]);}}