#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<map>
#define exp 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
int v[1100],w[1010],u[1010];
LL dp[550000];
int n,m;
int max(int a,int b){
return a>b?a:b;
}
void zeroset(int cost,int weight){
for(int i=n;i>=cost;i--)
dp[i]=max(dp[i],dp[i-cost]+weight);
}
//01背包
void complete(int cost,int weight){
for(int i=cost;i<=n;i++)
dp[i]=max(dp[i],dp[i-cost]+weight);
}
//完全背包
void multisett(int cost,int weight,int amount){
if(cost*amount>=n) complete(cost,weight);
else{
int k=1;
while(k<amount){
zeroset(k*cost,k*weight);
amount-=k;
k<<=1;
}
zeroset(amount*cost,amount*weight);
}
}
//多重背包
int main()
{
while(~scanf("%d%d",&m,&n))
{
for(int i=1;i<=m;i++)
scanf("%d%d%d",&v[i],&w[i],&u[i]);
memset(dp,0,sizeof(dp));
for(int i=1;i<=m;i++)
multisett(v[i],w[i],u[i]);
printf("%lld\n",dp[n]);
}
return 0;
}
背包问题万能板子
猜你喜欢
转载自blog.csdn.net/qq_41243063/article/details/82080695
今日推荐
周排行