多重背包问题1
作者:
殇ベ_11
,
2021-02-03 15:51:32
,
所有人可见
,
阅读 372
#include<iostream>
using namespace std;
const int N = 100220;
int dp[N << 1],sum[N << 1],w[N << 1],v[N << 1],n,m,s;
int read()
{
char c = getchar();
int f = 1, x = 0;
while(!isdigit(c)) {if(c == '-') f = -1;c = getchar();}
while(isdigit(c)){x = x * 10 + c - 48;c = getchar();}
return x * f;
}
int main()
{
n = read();
m = read();
for(int i=1;i<=n;i++)
{
v[i] = read();
w[i] = read();
sum[i] = read();
}
for(int i=1;i<=n;i++)
{
for(int j=m;j>=v[i];j--)
{
for(int k=1;k<=sum[i];k++)
{
if(j>=k*v[i]) //注意控制体积小于最大
{
dp[j]=max(dp[j],dp[j-v[i]*k]+k*w[i]);
}
}
}
}
cout<<dp[m];
}
nbnb