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. background背景色

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. HTML的优化

    HTML的优化 : 1).h标签的使用: 要注意的是,不论任何页面,h1标签只能出现一次,它是当前页面的主标题,权重最高, 所以要慎用 . 一般情况下,如果有关键词的话最好是在h1里面出现. h2是表 ...

  3. Clipboard 自动复制功能,ios复制失败,换方案 user-select: text ;长按复制 (ios 兼容,长按复制)

    Clipboard 自动复制功能,嵌套app内跳转的页面,ios 自动复制失败(该ios机子,微信,浏览器打开复制没有问题) 暂时换方案    user-select: text ;长按复制 (ios ...

  4. java表达式和三目运算符

    是由数字.运算符.数字分组符号(括号)等以能求得数值的有意义排列的序列; a + b 3.14 + a (x + y) * z + 100 boolean b= i < 10 && ...

  5. The 'decorators' plugin requires a 'decoratorsBeforeExport' option, ...(npm start报错)

    问题描述: 在npm start启动react项目的时候,出现了如下报错: The 'decorators' plugin requires a 'decoratorsBeforeExport' op ...

  6. css图片填充的几种方式

    当图片比例不固定时,想要让图片自适应,一般都会用background-size:cover/contain,但是这个只适用于背景图. img有个属性object-fit 属性值:object-fit: ...

  7. UVA 247"Calling Circles"(floyd求传递闭包+SCC)

    传送门 题意: 如果两个人相互打电话(直接或间接),则说他们在同一个电话圈里. (a,b) 表示 a 打给 b: 例如,(a,b),(b,c),(c,d),(d,a),则这四个人在同一个电话圈里: 输 ...

  8. H3C DHCP服务器显示及维护

  9. P1021 整数奇偶排序

    整数奇偶排序 题目出处:<信息学奥赛一本通>第二章上机练习6,略有改编 题目描述 告诉你包含 \(n\) 个数的数组 \(a\) ,你需要把他们按照"奇数排前面,偶数排后面:奇数 ...

  10. ipv6现状,加英文的中括号访问, ipv6测试http://test-ipv6.com

    加英文的中括号就可以,如[2001:4998:c:e33::1004],我发现这是yahoo首页.但并不是所有IPv6网站都可以通过IPv6地址访问,跟IPv4一样,网站服务器端可以只绑定域名,不接受 ...