题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2191
题目大意:
中文题。
思路:简单的多重背包。
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int max(int a,int b)
{
if(a>b)return a;
else return b;
}
int n,m,p[205],h[205],c[205];
int sum,dp[200005];
void zero(int cost,int weight)
{
for(int i=n;i>=cost;i--)
{dp[i]=max(dp[i],dp[i-cost]+weight);
}
}
void complet(int cost,int weight)
{
for(int i=cost;i<=n;i++)
{dp[i]=max(dp[i],dp[i-cost]+weight);
// printf("%d? ",dp[i]);
}
}
void multi(int cost ,int amount,int weight)
{
if(cost*amount>=n)
{
complet(cost,weight);
return;
}
int k=1;
while(k<amount)
{
zero(k*cost,k*weight);
amount-=k;
k=k*2;
}
zero(amount*cost,amount*weight);
}
int main()
{
int i,j,k;
int T;
scanf("%d",&T);
while(T--)
{
memset(dp,0,sizeof(dp));
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++)
scanf("%d%d%d",&p[i],&h[i],&c[i]);
for(i=1;i<=m;i++)
multi(p[i],c[i],h[i]);
printf("%d\n",dp[n]);
}
return 0;
}