HDU 2191 (多重背包)

水题,还可以用滚动数组~

#include <bits/stdc++.h>
using namespace std;
#define maxn 1111
#define INF 11111111

int dp[111]; //花i钱的最大收益
int tot, n, m, sum;

struct node {
    int w, cost;
}p[maxn];

int main () {
    //freopen ("in", "r", stdin);
    int t;
    scanf ("%d", &t);
    while (t--) {
        scanf ("%d%d", &n, &m);
        tot = 0;
        for (int id = 1; id <= m; id++) {
            int cnt, cost, w;
            scanf ("%d%d%d", &cost, &w, &cnt);
            for (int i = 1; i <= cnt; i <<= 1) {
                p[tot].w = i*w;
                p[tot].cost = i*cost;
                cnt -= i;
                tot++;
            }
            if (cnt) {
                p[tot].w = cnt*w;
                p[tot].cost = cnt*cost;
                tot++;
            }
        }
        memset (dp, 0, sizeof dp);
        for (int i = 0; i < tot; i++) {
            for (int j = n-p[i].cost; j >= 0; j--) {
                dp[j+p[i].cost] = max (dp[j+p[i].cost], dp[j]+p[i].w);
            }
        }
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            ans = max (ans, dp[i]);
        }
        printf ("%d\n", ans);
    }
    return 0;
}


你可能感兴趣的:(HDU 2191 (多重背包))