51 Nod 1086 多重背包问题(二进制优化)

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<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<algorithm>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n,W;
int w[105];//体积
int v[105];//价值
int cnt[105];//数量
int dp[50005];
void solve()
{
    for(int i=1;i<=n;i++)
    {
        int num=cnt[i];
        for(int k=1;num>0;k<<=1)
        {
            int tmp=min(k,num);
            for(int j=W;j>=w[i]*tmp;j--)
            {
                dp[j]=max(dp[j],dp[j-w[i]*tmp]+v[i]*tmp);
            }
            num-=tmp;
        }
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLIN
    scanf("%d%d",&n,&W);
    for(int i=1;i<=n;i++)scanf("%d%d%d",&w[i],&v[i],&cnt[i]);
    solve();
    printf("%d\n",dp[W]);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值