51nod 1086背包问题V2 (完全背包模板题)
1086 背包问题 V2
- 1 秒
- 131,072 KB
- 20 分
- 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 (完全背包模板题)的更多相关文章
- 51nod 1086 背包问题 V2 【二进制/多重背包】
1086 背包问题 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放 ...
- 51nod 1086 背包问题 V2(二进制优化多重背包)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1086 题解:怎么用二进制优化多重背包,举一个例子就明白了. ...
- 51nod 1086 背包问题 V2
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1086 思路 裸的多重背包 巩固一下刚学的骚操作 #include< ...
- 51Nod:1086背包问题 V2
1086 背包问题 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里 ...
- HDU 1114 Piggy-Bank(完全背包模板题)
完全背包模板题 #include<cstdio> #include<cstring> #include<algorithm> using namespace std ...
- HDU 2602 - Bone Collector - [01背包模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...
- HDU 2191 珍惜现在,感恩生活(多重背包模板题)
多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...
- 51nod 1028 大数乘法 V2 【FFT模板题】
题目链接 模板题.. #include<bits/stdc++.h> using namespace std; typedef int LL; typedef double db; nam ...
- POJ 3624 Charm Bracelet(01背包模板题)
题目链接 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 52318 Accepted: 21912 Descriptio ...
随机推荐
- hdu 1045 Fire Net(dfs)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- 深入java面向对象三:抽象类和接口(转载)
文章系转载,地址: http://blog.csdn.net/xw13106209/article/details/6923556 1.概述 一个软件设计的好坏,我想很大程度上取决于它的整体架 ...
- [转]1.2 java web的发展历史
前言 了解java web的发展历史和相关技术的演进历程,非常有助于加深对java web技术的理解和认识. 阅读目录 1.Servlet的出现 2.Jsp的出现 3.倡导了MVC思想的Servlet ...
- linux初始化中的错误处理
你必须记住一件事, 在注册内核设施时, 注册可能失败. 即便最简单的动作常常需要内存 分配, 分配的内存可能不可用. 因此模块代码必须一直检查返回值, 并且确认要求的操作 实际上已经成功. 如果在你注 ...
- js基础——流程控制语句
1.if语句 if(条件表达式){ 若条件成立只执行我,下面的分支不执行 }else if(条件表达式){ 语句; }else if(条件表达式){ 语句; }else{ ...
- 机器学习——EM
整理自: https://blog.csdn.net/woaidapaopao/article/details/77806273?locationnum=9&fps=1 EM算法是用于含有隐变 ...
- linux 自动检测 IRQ 号
驱动在初始化时最有挑战性的问题中的一个是如何决定设备要使用哪个 IRQ 线. 驱动需 要信息来正确安装处理. 尽管程序员可用请求用户在加载时指定中断号, 这是个坏做法, 因为大部分时间用户不知道这个号 ...
- CodeForces - 721D Maxim and Array (贪心)
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea ...
- Oracle生成批量清空表数据脚本
select 'DELETE FROM ' || a.table_name || '; --' || a.comments from user_tab_comments a where a.table ...
- Java 学习笔记(4)——面向对象
现在一般的语言都支持面向对象,而java更是将其做到很过分的地步,java是强制使用面向对象的写法,简单的写一个Hello Word都必须使用面向对象,这也是当初我很反感它的一点,当然现在也是很不喜欢 ...