1086 背包问题 V2

  1. 1 秒
  2. 131,072 KB
  3. 20 分
  4. 3 级题

题目描述

有N种物品,每种物品的数量为C1,C2......Cn。从中任选若干件放在容量为W的背包里,每种物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi为整数)。求背包能够容纳的最大价值。

 

输入

第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)

输出

输出可以容纳的最大价值。

输入样例

3 6
2 2 5
3 3 8
1 4 1

输出样例

9

题解:
  多重背包模板题。二进制优化。
#include <stdio.h>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <iostream>
#include <algorithm>
#define ll long long
#define ull unsigned ll
#define exp 1e-8
using namespace std;
const int INF = 2e9 + ;
const int mod = 1e9 + ;
int c[],w[],num[],f[]={};
int main() {
int V,N,wi,ci,numi,cnt = ;
scanf("%d%d",&N,&V);
int cnt = ;
for (int i = ; i < N; i++){
scanf("%d%d%d",&wi,&ci,&numi);
for (int j = ; j <= numi; j<<=){
w[cnt] = j*wi;
c[cnt] = j*ci;
numi-=j;
cnt++;
}
if (numi){
w[cnt] = numi*wi;
c[cnt] = numi*ci;
cnt++;
}
}
for (int i = ; i < cnt; i++){
for (int j = V; j >= w[i]; j--){
f[j] = max(f[j],f[j-w[i]]+c[i]);
}
}
printf("%d\n",f[V]);
return ;
}

51nod 1086背包问题V2 (完全背包模板题)的更多相关文章

  1. 51nod 1086 背包问题 V2 【二进制/多重背包】

    1086 背包问题 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放 ...

  2. 51nod 1086 背包问题 V2(二进制优化多重背包)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1086 题解:怎么用二进制优化多重背包,举一个例子就明白了. ...

  3. 51nod 1086 背包问题 V2

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1086 思路  裸的多重背包 巩固一下刚学的骚操作 #include< ...

  4. 51Nod:1086背包问题 V2

    1086 背包问题 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里 ...

  5. HDU 1114 Piggy-Bank(完全背包模板题)

    完全背包模板题 #include<cstdio> #include<cstring> #include<algorithm> using namespace std ...

  6. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  7. HDU 2191 珍惜现在,感恩生活(多重背包模板题)

    多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...

  8. 51nod 1028 大数乘法 V2 【FFT模板题】

    题目链接 模板题.. #include<bits/stdc++.h> using namespace std; typedef int LL; typedef double db; nam ...

  9. POJ 3624 Charm Bracelet(01背包模板题)

    题目链接 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 52318   Accepted: 21912 Descriptio ...

随机推荐

  1. 2019-10-23-WPF-使用-SharpDx-异步渲染

    title author date CreateTime categories WPF 使用 SharpDx 异步渲染 lindexi 2019-10-23 21:18:38 +0800 2018-0 ...

  2. 【codeforces 789B】Masha and geometric depression

    [题目链接]:http://codeforces.com/contest/789/problem/B [题意] 让你一个一个地写出等比数列的每一项 (注意是一个一个地写出); 有m个数字不能写; 且数 ...

  3. linux scull 函数open 方法

    open 方法提供给驱动来做任何的初始化来准备后续的操作. 在大部分驱动中, open 应当 进行下面的工作: 检查设备特定的错误(例如设备没准备好, 或者类似的硬件错误 如果它第一次打开, 初始化设 ...

  4. JSON怎样添加注释

    今天在写一个程序的时候发现了一个问题,在json文件中添加注释之后,程序就出现bug了 于是,去搜了一下这个问题的相关解释,在这里和大家分享一下: JSON为什么不能添加注释? 这位外国友人给出的解释 ...

  5. 【75.28%】【codeforces 764B】Decoding

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【74.00%】【codeforces 747A】Display Size

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. thinkjs解决跨域

    this.header("Access-Control-Allow-Origin", "*"); 将上面的代码在请求发送之前执行即可 如果不知道放在哪里 可以参 ...

  8. phpcms 增加备案号、联系方式等字段

    准备好记事本或者dreamweaver或者其它文本编辑器 打开\phpcms\languages\zh-cn\admin.lang.php PHPCMS的中文语言定义文件. 查找“site_manag ...

  9. dotnet core 输出调试信息到 DebugView 软件

    本文告诉大家如何在 dotnet core 输出调试信息到 DebugView 软件 在之前告诉小伙伴,如何在 WPF 输出调试信息到 DebugView 软件,请看文章 WPF 调试 获得追踪输出 ...

  10. 2018-2-13-win10-uwp-判断设备类型

    title author date CreateTime categories win10 uwp 判断设备类型 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 1 ...