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


php

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
 

 題意:ios

有n個項目,每一個項目涉及到最多3個不一樣的領域,有m個工程師每一個工程師掌握最多兩個不一樣的領域,每一個工程師只能參與一個項目,問最多能完成幾個項目。(數據範圍)atom

代碼:spa

//把工程師狀壓而後枚舉處理到第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;
}
相關文章
相關標籤/搜索