51nod-1086 背包问题(多重背包)
2016-09-03 15:12
141 查看
原题链接
1086 背包问题 V2
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
收藏
关注
有N种物品,每种物品的数量为C1,C2......Cn。从中任选若干件放在容量为W的背包里,每种物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi为整数)。求背包能够容纳的最大价值。
Input
Output
Input示例
Output示例
多重背包问题,用二进制拆分
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int w[105], p[105], c[105];
int dp[50005];
int main(){
// freopen("in.txt", "r", stdin);
int N, W;
scanf("%d%d", &N, &W);
for(int i = 0; i < N; i++){
scanf("%d%d%d", w+i, p+i, c+i);
}
int w1, p1;
for(int i = 0; i < N; i++){
int j;
for(j = 1; j <= c[i]; j <<= 1){
w1 = j * w[i], p1 = j * p[i];
for(int h = W; h >= w1; h--){
dp[h] = max(dp[h], dp[h - w1] + p1);
}
c[i] -= j;
}
j = c[i];
w1 = j * w[i], p1 = j * p[i];
for(int h = W; h >= w1; h--){
dp[h] = max(dp[h], dp[h - w1] + p1);
}
}
printf("%d\n", dp[W]);
return 0;
}
1086 背包问题 V2
基准时间限制: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 <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int w[105], p[105], c[105];
int dp[50005];
int main(){
// freopen("in.txt", "r", stdin);
int N, W;
scanf("%d%d", &N, &W);
for(int i = 0; i < N; i++){
scanf("%d%d%d", w+i, p+i, c+i);
}
int w1, p1;
for(int i = 0; i < N; i++){
int j;
for(j = 1; j <= c[i]; j <<= 1){
w1 = j * w[i], p1 = j * p[i];
for(int h = W; h >= w1; h--){
dp[h] = max(dp[h], dp[h - w1] + p1);
}
c[i] -= j;
}
j = c[i];
w1 = j * w[i], p1 = j * p[i];
for(int h = W; h >= w1; h--){
dp[h] = max(dp[h], dp[h - w1] + p1);
}
}
printf("%d\n", dp[W]);
return 0;
}
相关文章推荐
- 51nod 1086 背包问题 V2 (多重背包问题)
- 【多重背包】51nod 1086 背包问题 V2
- 51NOD-1086 背包问题 V2(多重背包)
- 51nod 1086 背包问题 V2(多重背包)
- AC日记——背包问题 V2 51nod 1086
- 51nod 1086 背包问题 V2
- 51nod 1086 背包问题 V2
- 【51Nod 1086】背包问题 V2
- 51nod 1086 背包问题 V2
- 51nod 1086 背包问题V2 (巧妙dp,二进制)
- 51Nod 1086【背包问题 V2】
- 51Nod-1086-背包问题 V2
- 51nod 1086 背包问题 V2 多重背包
- 51Nod 1086背包问题V2
- 51nod 1086 背包问题 V2(多重背包)
- 51NOD-1086 背包问题 V2
- 51Nod:1086背包问题 V2
- 51nod 1086 背包问题V2 动态规划
- 51Nod 1086 背包问题 V2(二进制多重背包)
- 51nod 1086 背包问题 V2(多重背包问题)