hdu 6006 Engineer Assignment(状压DP)

Engineer Assignment

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


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  ith line 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
  
  
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个工程师,每个工程师会一些领域,问这些工程师最多完成多少任务
题解:先预处理出能第i个任务可以由哪些人一起完成,因为人数不多,所以可以用状态压缩,然后dp[i][j]来表示表前i个任务,状态为j时,最多完成的任务数

一开始用一维表状态一直wa   改成二维 第一维是前i个工程的最大能完成数目的最优状态  当考虑第i+1个时 可能选或者不选

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
#include <bits/stdc++.h>
using namespace std;
const int N = 120;
typedef long long LL;
int vis[N];
int  dp[20][1<<15], a[20][30], b[20][30];
void init()
{
    memset(dp,0,sizeof(dp));
    return ;
}
vector<int>p[N];

int main()
{
    //cout<<(1<<30)<<endl;
    int t, ncase=1;
    scanf("%d", &t);
    while(t--)
    {
        int n,m;
        scanf("%d %d", &n, &m);
        for(int i=0; i<=n; i++) p[i].clear();
        init();
        int  x, y;
        for(int i=1; i<=n; i++)
        {
            scanf("%d", &x);
            a[i][0]=x;
            for(int j=1; j<=x; j++)  scanf("%d", &a[i][j]);
        }
        for(int i=0; i<m; i++)
        {
            scanf("%d", &x);
            b[i][0]=x;
            for(int j=1; j<=x; j++)  scanf("%d", &b[i][j]);
        }
        for(int j=1; j<=n; j++)
        {
            for(int i=0; i<(1<<m); i++)
            {
                memset(vis,0,sizeof(vis));
                int cnt=0;
                for(int j=0; j<m; j++)
                {
                    if(i&(1<<j))
                    {
                        cnt++;
                        for(int k=1; k<=b[j][0]; k++)
                        {
                            vis[b[j][k]]=1;
                        }
                    }

                }
                int flag=0;
                for(int k=1; k<=a[j][0]; k++)
                {
                    if(!vis[a[j][k]])
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==0) p[j].push_back(i);
            }
        }
        int ans=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=0; j<(1<<m); j++)
            {
                for(int k=0; k<p[i].size(); k++)
                {
                    if((j|p[i][k])==j)dp[i][j]=max(dp[i][j],dp[i-1][j-p[i][k]]+1);
                }
                dp[i][j]=max(dp[i][j],dp[i-1][j]);
                ans=max(ans,dp[i][j]);
            }
        }
        printf("Case #%d: %d\n",ncase++, ans);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值