1.题目
http://acm.hdu.edu.cn/showproblem.php?pid=2191
2.分析
原本是一个多重背包的问题,不过感觉数据量比较小,然后就用01背包的思路模拟多重背包,竟然也过了3.复杂度
O(VN),V为所有补品和,N为所有物品个数4.涉及内容
动态规划5.感想
有时候可以尝试用简单的方法的组合来解决复杂的问题6.代码
#include <iostream>
using namespace std;
long f[101];
int N;
void ZeroOnePack(int cost,int weight)
{
for(int i=N;i>=cost;--i)
{
f[i]=f[i]>(f[i-cost]+weight)?f[i]:(f[i-cost]+weight);
}
}
int main()
{
freopen("in.txt","r",stdin);
int C,m,p,h,c;
int max;
cin>>C;
while(C--)
{
memset(f,0,101*sizeof(long));
max=0;
cin>>N>>m;
for(int i=0;i<m;++i)
{
cin>>p>>h>>c;
for(int j=0;j<c;++j)
{
ZeroOnePack(p,h);
}
}
for(int i=1;i<=N;++i)
{
max=max>f[i]?max:f[i];
}
cout<<max<<endl;
}
return 0;
}