HDU 6006 状压dp

Engineer Assignment

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


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 ithengineer.
 

 

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.
 

 

Source
 

 题意:

有n个项目,每个项目涉及到最多3个不同的领域,有m个工程师每个工程师掌握最多两个不同的领域,每个工程师只能参与一个项目,问最多能完成几个项目。(数据范围)

代码:

//把工程师状压然后枚举处理到第i个项目所用的状态j,然后如果做第i个项目那么再枚举k状态来完成i项目,j^k状态处理前i-1的项目,这样再加上
//100组样例就超时了,可以预处理出来每一个项目可以用那些状态来完成,所以第三层循环最多120个。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int t,n,m,f[12][1100],a[12][2],b[12][3],sol[12][1100];
bool vis[110],has[1100][1100];
void get_has()
{
    for(int i=0;i<(1<<10);i++){
        for(int j=0;j<(1<<10);j++){
            has[i][j]=1;
            for(int k=0;k<10;k++){
                if(!(i&(1<<k))&&(j&(1<<k))) has[i][j]=0;
            }
        }
    }
}
bool check(int sta,int x)
{
    memset(vis,0,sizeof(vis));
    vis[0]=1;
    for(int i=0;i<m;i++){
        if(sta&(1<<i)){
            vis[a[i][0]]=vis[a[i][1]]=1;
        }
    }
    if(vis[b[x][0]]&&vis[b[x][1]]&&vis[b[x][2]]) return 1;
    return 0;
}
void get_sol()
{
    for(int i=1;i<=n;i++){
        sol[i][0]=0;
        for(int j=0;j<(1<<m);j++){
            if(check(j,i)){
                sol[i][0]++;
                sol[i][sol[i][0]]=j;
            }
        }
    }
}
int main()
{
    get_has();
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++){
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            int x;
            scanf("%d",&x);
            for(int j=0;j<x;j++) scanf("%d",&b[i][j]);
        }
        for(int i=0;i<m;i++){
             int x;
             scanf("%d",&x);
             for(int j=0;j<x;j++) scanf("%d",&a[i][j]);
        }
        get_sol();
        memset(f,0,sizeof(f));
        for(int i=1;i<=n;i++){
            for(int j=1;j<(1<<m);j++){
                f[i][j]=max(f[i][j],f[i-1][j]);
                for(int k=1;k<=sol[i][0];k++){
                    if(!has[j][sol[i][k]]) continue;
                    f[i][j]=max(f[i][j],f[i-1][j^sol[i][k]]+1);
                }
            }
        }
        printf("Case #%d: %d\n",cas,f[n][(1<<m)-1]);
    }
    return 0;
}

 

posted @ 2017-12-13 17:28  luckilzy  阅读(297)  评论(0编辑  收藏  举报