链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191
题目的大意也很清楚,就是给你一定的钱,还有各种米的重量、费用
问你如何买才能买到最大重量的大米
AC代码,应用多重背包模板:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define max(a,b) a>b?a:b
int p[1050],h[1050],c[1050],dp[1050];
int n,m;
void ZeroOnePack(int cost,int weight) //01背包
{
int i;
for(i=n;i>=cost;i--) //n为总钱数,cost为开销
dp[i]=max(dp[i],dp[i-cost]+weight); //
}
void CompletePack(int cost,int weight) //完全背包
{
int i;
for(i=cost;i<=n;i++)
dp[i]=max(dp[i],dp[i-cost]+weight);
}
void MultiplePack(int cost,int weight,int amount) //多重背包,2机制算法,物品金额,重量,数量
{
if(cost*amount>=n)
{
CompletePack(cost,weight); //如果该物品的数量和单位金额总数大于总现金
}
else
{
int k=1;
while(k<=amount)
{
ZeroOnePack(k*cost,k*weight); //取k件
amount-=k;
k<<=1; // *2
}
ZeroOnePack(amount*cost,amount*weight);
}
}
int main()
{
int C;
scanf("%d",&C);
while(C--)
{
int i;
memset(dp,0,sizeof(dp));
scanf("%d %d",&n,&m); //总钱数,种类
for(i=0;i<m;i++)
{
scanf("%d %d %d",&p[i],&h[i],&c[i]); //p是价格,h是重量, c是数量
MultiplePack(p[i],h[i],c[i]);
}
printf("%d\n",dp[n]);
}
return 0;
}
/*
1
8 2 有的钱和品种
2 100 4 价格,重量,数量
4 100 2
400
怎么样买才能使重量最大
*/