Hdu 2191 珍惜现在,感恩生活 (多重背包)

来源:互联网 发布:2017 行政区域数据库 编辑:程序博客网 时间:2024/05/17 08:20

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191

简单的多重背包。

#include <cstdio>#include <cstring>#define max(x,y) (x)>(y)?(x):(y)int m,v;int f[105],c[105],w[105],n[105];void ZeroOnePack (int cost, int weight){for (int j=v;j>=cost;j--)f[j] = max(f[j], f[j-cost] + weight);}void CompletePack (int cost, int weight){for (int j=cost;j<=v;j++)f[j] = max(f[j], f[j-cost] + weight);}void MultiplePack (int cost, int weight, int all){if (cost*all >= v)     //如果足够,转化为完全背包{CompletePack (cost, weight);return ;}int k=1;while (k < all)         //否则利用二进制的思想将其划分为若干个小物品{ZeroOnePack (k*cost, k*weight);all-=k;k<<=1;}ZeroOnePack (all*cost,all*weight);}int main(){int T;scanf("%d",&T);while (T--){scanf("%d%d",&v,&m);memset(f,0,sizeof(f));int i;for (i=1;i<=m;i++)scanf("%d%d%d",&c[i],&w[i],&n[i]);for (i=1;i<=m;i++)MultiplePack(c[i], w[i], n[i]);printf("%d\n",f[v]);}return 0;}