hdu3535---AreYouBusy(混合分组背包,有坑点)

混合的分组背包,如果任意取,就是01背包
如果至多取一个,就是普通的分组背包
如果至少一个,就要考虑第一次和非第一次拿,注意由于体积可以为0,所以先得考虑非第一次拿的,不然某组的同一个物品会被计算2次

/************************************************************************* > File Name: hdu3535.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年05月10日 星期日 16时06分19秒 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int dp[110][110];
int type[110];
vector <PLL> jobs[110];

int main() {
    int n, t, m, c, g;
    while (~scanf("%d%d", &n, &t)) {
        memset(dp, -1, sizeof(dp));
        for (int i = 1; i <= n; ++i) {
            jobs[i].clear();
            scanf("%d%d", &m, &type[i]);
            for (int j = 1; j <= m; ++j) {
                scanf("%d%d", &c, &g);
                jobs[i].push_back(make_pair(c, g));
            }
        }
        for (int i = 0; i <= t; ++i) {
            dp[0][i] = 0;
        }
        for (int i = 1; i <= n; ++i) {
            int size = jobs[i].size();
            if (type[i] == 0) {
                for (int j = 0; j < size; ++j) {
                    for (int k = t; k >= jobs[i][j].first; --k) {
                        if (dp[i][k - jobs[i][j].first] != -1) {
                            dp[i][k] = max(dp[i][k], dp[i][k - jobs[i][j].first] + jobs[i][j].second);
                        }
                        if (dp[i - 1][k - jobs[i][j].first] != -1) {
                            dp[i][k] = max(dp[i][k], dp[i - 1][k - jobs[i][j].first] + jobs[i][j].second);
                        }
                    }
                }
            }
            else if (type[i] == 1) {
                for (int j = 0; j <= t; ++j) {
                    dp[i][j] = dp[i - 1][j];
                }
                for (int j = 0; j <= t; ++j) {
                    for (int k = 0; k < size; ++k) {
                        if (j < jobs[i][k].first) {
                            continue;
                        }
                        if (dp[i - 1][j - jobs[i][k].first] != -1) {
                            dp[i][j] = max(dp[i][j], dp[i - 1][j - jobs[i][k].first] + jobs[i][k].second);
                        }
                    }
                }
            }
            else {
                for (int j = 0; j <= t; ++j) {
                    dp[i][j] = dp[i - 1][j];
                }
                for (int j = 0; j < size; ++j) {
                    for (int k = t; k >= 0; --k) {
                        if (k < jobs[i][j].first) {
                            break;
                        }
                        if (dp[i][k - jobs[i][j].first] != -1) {
                            dp[i][k] = max(dp[i][k], dp[i][k - jobs[i][j].first] + jobs[i][j].second);
                        }
                    }
                }
            }
        }
        printf("%d\n", dp[n][t]);
    }
    return 0;
}

你可能感兴趣的:(dp)