1、正如本题的名称,我WA了将近10次,但是在看了《背包九讲》前四章后一次AC了~所以说,有时候不是智商的问题,而是懂得太少。
2、注意初始化!注意数组不要越界!其他没什么了。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct rice{
int p,h,c;
}a[110];
int dp[110],n,m;
void ZeroOnePack(int cost,int weight){
for(int v=n;v>=cost;v--)
dp[v]=max(dp[v],dp[v-cost]+weight);
}
void CompletePack(int cost,int weight){
for(int v=cost;v<=n;v++)
dp[v]=max(dp[v],dp[v-cost]+weight);
}
void MultiplePack(int cost,int weight,int amount){
if(cost*amount>=n){
CompletePack(cost,weight);
return;
}
int k=1;
while(k<amount){
ZeroOnePack(k*cost,k*weight);
amount-=k;
k*=2;
}
ZeroOnePack(amount*cost,amount*weight);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(dp,0,sizeof(dp));
for(int i=1;i<=m;i++)
scanf("%d%d%d",&a[i].p,&a[i].h,&a[i].c);
for(int i=1;i<=m;i++){
MultiplePack(a[i].p,a[i].h,a[i].c);
}
printf("%d\n",dp[n]);
}
return 0;
}