hdu 6006 Engineer Assignment(状压dp)

来源:互联网 发布:阿里云 虚拟主机 jsp 编辑:程序博客网 时间:2024/06/05 08:15

题目链接

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.

以后学乖了,看到这么小的数据量还是先去想状压吧……

比赛的时候其实后来想到了状压的方法,但是到了后面
时间也不多了,思路也还没有那么清晰,再加上代码比较挫,没有写出来。

大致的思路就是:首先进行预处理,把每个工程师选与不选压缩成一个状态,然后用一个vector数组存储完成每个任务工程师的状态集合。
dp[i][s]代表前i个任务,状态为s时,最多完成的任务数。
具体递推关系可以看代码。

预处理是这道题稍微复杂一点的地方,但其实思路清晰之后也很好写,之前用了map,但是一直t,后来用数组重写了,但可能不是map的锅(也有可能是我写挫了),毕竟最后过的时间还是很快的。
一定要记得初始化!

#include<iostream>#include<queue>#include<cstdio>#include<cstring>#include<string>#include <cmath>#include <map>#include <set>#include <vector>#include <iterator>using namespace std;const double eps = 1e-6;const double pi = acos(-1.0);const int INF = 0x3f3f3f3f;#define ll long long#define CL(a) memset(a,0,sizeof(a))vector<int> a[20];int d[20][10],c[20][10];int p[120];int dp[20][1<<11];int main(){    int t,n,m;    cin>>t;    int cas=0;    while(t--)    {        cin>>n>>m;        memset(c,0,sizeof(c));        memset(d,0,sizeof(d));        memset(p,0,sizeof(p));        memset(dp,0,sizeof(dp));        for(int i=1;i<=n;i++)        {            a[i].clear();        }       for(int i=1;i<=n;i++)       {           cin>>c[i][0];           for(int j=1;j<=c[i][0];j++)           {               cin>>c[i][j];           }       }       for(int i=0;i<m;i++)       {           cin>>d[i][0];           for(int j=1;j<=d[i][0];j++)           {               cin>>d[i][j];           }       }       for(int i=1;i<=n;i++)       {           for(int s=0;s<(1<<m);s++)           {               int cnt=0;               memset(p,0,sizeof(p));               for(int k=0;k<m;k++)               {                   if(s&(1<<k))                   {                       cnt++;                       for(int j=1;j<=d[k][0];j++)                        p[d[k][j]]=1;                   }               }               if(cnt>3) continue;               int flag=1;               for(int j=1;j<=c[i][0];j++)                if(p[c[i][j]]==0) flag=0;               if(flag) a[i].push_back(s);           }       }        for(int i=1; i<=n; i++)        {            for(int s=0; s<(1<<m); s++)            {                for(int j=0; j<a[i].size(); j++)                {                    if((s|a[i][j])==s)                    {                            dp[i][s]=max(dp[i-1][s-a[i][j]]+1,dp[i][s]);  //这里代表的是一定会完成第i个任务的情况。                    }                }                dp[i][s]=max(dp[i][s],dp[i-1][s]);  //加上这句就表示可以不选用第i个任务            }        }        printf("Case #%d: ",++cas);        cout<<dp[n][(1<<m)-1]<<endl;    }    return 0;}
1 0
原创粉丝点击