基准时间限制:1 秒 空间限制:131072 KB 分值: 40
难度:4级算法题
有N种物品,每种物品的数量为C1,C2......Cn。从中任选若干件放在容量为W的背包里,每种物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi为整数)。求背包能够容纳的最大价值。
Input
第1行,2个整数,N和W中间用空格隔开。N为物品的种类,W为背包的容量。(1 <= N <= 100,1 <= W <= 50000) 第2 - N + 1行,每行3个整数,Wi,Pi和Ci分别是物品体积、价值和数量。(1 <= Wi, Pi <= 10000, 1 <= Ci <= 200)
Output
输出可以容纳的最大价值。
Input示例
3 6 2 2 5 3 3 8 1 4 1
Output示例
9
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn=50050;
int a[maxn],b[maxn],dp[maxn];
int main()
{
int n,m,x,y,z,l;
scanf("%d %d",&n,&m);
l=0;
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++){
scanf("%d %d %d",&x,&y,&z);
int t=1;
while(z>0){
if(z>=t){
a[++l]=t*x;
b[l]=t*y;
z=z-t;
}else{
a[++l]=z*x;
b[l]=y*z;
z=0;
}
t=t*2;
}
}
for(int i=1;i<=l;i++){
for(int j=m;j>=a[i];j--){
dp[j]=max(dp[j],dp[j-a[i]]+b[i]);
}
}
printf("%d\n",dp[m]);
return 0;
}
608

被折叠的 条评论
为什么被折叠?



