1086 背包

1086 背包问题 V2

有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

思路:多重背包裸
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int n,m;
 5 int a[10003],b[10003];
 6 int dp[50004];
 7 
 8 int main(){
 9     scanf("%d%d",&n,&m);
10     int w,p,c;
11     int l=0;
12     while(n--){
13         scanf("%d%d%d",&w,&p,&c);
14         for(int i=1;i<=c;i<<=1){
15             a[l]=i*w;
16             b[l++]=i*p;
17             c-=i;
18         }
19         if(c>0){
20             a[l]=c*w;
21             b[l++]=c*p;
22         }
23     }
24     for(int i=0;i<l;i++){
25         for(int j=m;j>=a[i];j--)
26             dp[j]=max(dp[j],dp[j-a[i]]+b[i]);
27     }
28     printf("%d\n",dp[m]);
29 }

 

posted on 2017-06-19 19:48  hhhhx  阅读(155)  评论(0编辑  收藏  举报

导航