1. 程式人生 > >HDU 6006 Engineer Assignment(狀壓dp)

HDU 6006 Engineer Assignment(狀壓dp)

Engineer Assignment

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

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

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

這題唯一的難點就是你是否能想到用狀壓dp。

題意:

給你n(n<=10)個工程,每個工程需要a[i](a[i]<=3)種能力。給出其需要的能力的編號。m(m<=10)個工程師。每個工程師擁有b[i]種(b[i]<=2)能力,給出其擁有的能力編號。問你怎麼安排工程師,使能完成的工程數量最多。

思路:狀壓dp亂搞就行。我是預處理每個工程師的狀態能否完成這項工程,然後三個for列舉每一個工程和分配工程師的狀態和能完成這個工程的狀態。這題可以說是水題了。

程式碼:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=100010;
int a[15][150];
int c[15][150];
int aa[20],cc[20],n,m;
int jud[1050][15];
int ans,tmp,cnt;
int dp[15][1050];
bool ok[150];
void init()
{
    memset(jud,0,sizeof(jud));
    for(int i=0;i<(1<<m);i++)
    {
        memset(ok,0,sizeof(ok));
        for(int j=1;j<=m;j++)
        if(i&(1<<(j-1))){
            for(int k=1;k<=cc[j];k++)
            ok[c[j][k]]=1;
        }
        for(int j=1;j<=n;j++)
        {
        int k=1;
        for(;k<=aa[j];k++)
        if(!ok[a[j][k]]) break;
        if(k>aa[j]) jud[i][j]=1;
        }
    }
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        memset(dp,-1,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&aa[i]);
            for(int j=1;j<=aa[i];j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&cc[i]);
            for(int j=1;j<=cc[i];j++)
            {
                scanf("%d",&c[i][j]);
            }
        }
        init();
        int nn=(1<<m);
        ans=0;
        dp[0][0]=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<nn;j++)
            {
                dp[i][j]=max(dp[i][j],dp[i-1][j]);
                for(int k=0;k<nn;k++)
                if(!(k&j)&&jud[k][i]){
                    dp[i][j|k]=max(dp[i][j|k],dp[i-1][j]+1);
                }
            }
        }
        ans=0;
        for(int i=1;i<=n;i++)
        for(int j=0;j<nn;j++)
        ans=max(ans,dp[i][j]);
        printf("Case #%d: %d\n",cas++,ans);
    }
    return 0;
}