hdu 2191 (多重背包)

来源:互联网 发布:c语言 rand 编辑:程序博客网 时间:2024/04/29 16:53

多重背包模板题。

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;const int maxn = 111;int n, m, V, f[maxn], cv[maxn], val[maxn], num[maxn];void ZeroOne(int cost, int value){    for(int j = V; j >= cost; j--)        f[j] = max(f[j], f[j-cost] + value);}void Complete(int cost, int value){    for(int j = cost; j <= V; j++)        f[j] = max(f[j], f[j-cost] + value);}void Multiple(int cost, int value, int amount){    int k = 1;    if(cost * amount >= V)  Complete(cost, value);    else    {        while(amount >= k)        {            ZeroOne(cost * k, value * k);            amount -= k;            k *= 2;        }        ZeroOne(cost * amount, value * amount);    }}void dp(){    for(int i=1; i<=n; i++)        Multiple(cv[i], val[i], num[i]);}int main(){    int t;    scanf("%d", &t);    while(t--)    {        scanf("%d%d", &V, &n);        for(int i=1; i<=n; i++)            scanf("%d%d%d", &cv[i], &val[i], &num[i]);        memset(f, 0, sizeof(f));        dp();        printf("%d\n", f[V]);    }}


原创粉丝点击