题目大意:悼念512...
思路:多重背包...不优化也看过,数据弱...
AC Program:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int n,m;
int v;
int p[110],h[110],c[110];
int dp[110];
void ZeroOnePack(int cost,int weight)
{
for(int i=v;i>=cost;i--)
if(dp[i]<dp[i-cost]+weight)
dp[i]=dp[i-cost]+weight;
}
void CompletePack(int cost,int weight)
{
for(int i=cost;i<=v;i++)
if(dp[i]<dp[i-cost]+weight)
dp[i]=dp[i-cost]+weight;
}
void MultiplePack(int cost,int weight,int amount)
{
if(cost*amount>=v)
CompletePack(cost,weight);
else
{
for(int k=1;k<amount;)
{
ZeroOnePack(k*cost,k*weight);
amount-=k;
k<<=1;
}
ZeroOnePack(amount*cost,amount*weight);
}
}
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&p[i],&h[i],&c[i]);
}
v=n;
memset(dp,0,sizeof(dp));
for(int i=0;i<m;i++)
MultiplePack(p[i],h[i],c[i]);
printf("%d\n",dp[v]);
}
//system("pause");
return 0;}