HDU - 6006 Engineer Assignment 状压DP

Engineer Assignment

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

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.

解题思路:

预处理出每个任务需要的人员组合,以\(d[i][j]\)表示经过前i个任务状态j下完成的任务数,如果第i个任务不完成,那么\(dp[i][j]=dp[i-1][j]\)直接转移,要是完成第i个任务,那么当前状态j由各种能完成该任务的状态k转移来,有\(j|k==j\),那么\(dp[i][j]=dp[i-1][j-k]+1\)

具体实现:

#include <bits/stdc++.h>
using namespace std;
/*    freopen("k.in", "r", stdin);
    freopen("k.out", "w", stdout); */
//clock_t c1 = clock();
//std::cerr << "Time:" << clock() - c1 <<"ms" << std::endl;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define de(a) cout << #a << " = " << a << endl
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int, int> VII;
#define inf 0x3f3f3f3f
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MAXN = 1e6 + 7;
const ll MAXM = 1e6 + 7;
const ll MOD = 1e9 + 7;
const double eps = 1e-6;
const double pi = acos(-1.0);
int dp[15][1 << 13];
vector<int> c[15], d[15], v[15];
void init()
{
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i <= 14; i++)
    {
        c[i].clear();
        d[i].clear();
        v[i].clear();
    }
}
int main()
{
    int t;
    scanf("%d", &t);
    for (int tt = 1; tt <= t; tt++)
    {
        init();
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++)
        {
            int num;
            scanf("%d", &num);
            while (num--)
            {
                int x;
                scanf("%d", &x);
                c[i].push_back(x);
            }
        }
        for (int i = 1; i <= m; i++)
        {
            int num;
            scanf("%d", &num);
            while (num--)
            {
                int x;
                scanf("%d", &x);
                d[i].push_back(x);
            }
        }
        /* 预处理第i个任务能由哪几个状态完成 */
        for (int i = 1; i <= n; i++)
        {
            for (int j = 0; j <= (1 << m) - 1; j++) //状态
            {
                map<int, int> mp;
                for (int k = 0; k <= m - 1; k++)
                {
                    if ((1 << k) & j) //第k人在j状态内
                    {
                        for (auto p : d[k + 1]) //第k人的专业标记
                            mp[p] = 1;
                    }
                }
                for (auto k : c[i]) //是不是能完成
                {
                    if (!mp[k]) //不能完成
                        goto where;
                }
                v[i].push_back(j); //状态能完成
            where:
                continue;
            }
        }
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 0; j <= (1 << m) - 1; j++)
            {
                for (auto k : v[i])
                {
                    if ((j | k) == j) //能够完成组合
                        dp[i][j] = max(dp[i - 1][j - k] + 1, dp[i][j]);
                }
                dp[i][j] = max(dp[i][j], dp[i - 1][j]); //不完成该任务
            }
        }
        printf("Case #%d: %d\n", tt, dp[n][(1 << m) - 1]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/graytido/p/11457762.html