http://acm.hdu.edu.cn/showproblem.php?pid=2191
这道题是一个赤裸裸的多重背包问题;给你模板直接套就行了~
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAX = 1000;
int v,cost[MAX],weight[MAX],amount[MAX];
int f[MAX];
void ZeroOnePack(int cost,int weight)
{
int i;
for(i=v;i>=cost;i--)
{
f[i] = max(f[i],f[i-cost]+weight);
}
}
void CompletePack(int cost,int weight)
{
int i;
for(i=cost;i<=v;i++)
{
f[i] = max(f[i],f[i-cost]+weight);
}
}
void MultiplePack(int cost,int weight,int amount)
{
if(v <= cost*amount)
{
CompletePack(cost,weight);
return;
}
else
{
int k = 1;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight);
amount = amount - k;
k = k*2;
}
ZeroOnePack(amount*cost,amount*weight);
}
}
int main()
{
int c,m;
cin>>c;
while(c--)
{
cin>>v>>m;
memset(f,0,sizeof(f));
for(int i=1;i<=m;i++)
{
cin>>cost[i]>>weight[i]>>amount[i];
}
for(int i=1;i<=m;i++)
MultiplePack(cost[i],weight[i],amount[i]);
cout<<f[v]<<endl;
}
return 0;
}