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 ...
随机推荐
- sleep usleep nanosleep alarm setitimer使用
sleep使用的是alarm之类的定时器,定时器是使得进程被挂起,使进程处于就绪的状态. signal+alarm定时器 alarm参数的类型为uint, 并且不能填0 #include <st ...
- MYSQL设置远程账户登陆总结,mysql修改、找回密码、增加新用户,MySQL数据库的23个注意事项
1.5 设置及修改Mysql root用户密码1 设置密码方法mysqladmin -u root password '123456'mysqladmin -u root -p'123456' pas ...
- uni-app学习记录07-生命周期
<template> <view class="content"> 我是首页 </view> </template> <scr ...
- dotnet 设计规范 · 数组定义
本文告诉大家数组定义需要知道的规范,本文翻译 docs dotnet ✓ 建议在公开的 API 使用集合而不是数组.集合可以提供更多的信息. X 不建议设置数组类型的字段为只读.虽然用户不能修改字段, ...
- [转]什么是CNN、RNN、LSTM
. 全连层 每个神经元输入: 每个神经元输出: (通过一个激活函数) 2. RNN(Recurrent Neural Network) 与传统的神经网络不通,RNN与时间有关. 3. LSTM(Lon ...
- Vue 父组件与子组件的传值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- H3C RIP基本配置举例
- P1086 最大素数积
题目描述 我们称一个整数 \(x\) 是"素数积"当且仅当 \(x = a \times b\) 并且 \(a\) 和 \(b\) 都是素数. 现在告诉你一个数 \(N(1 \le ...
- 2019-8-31-dotnet-Framework-源代码-·-Ink
title author date CreateTime categories dotnet Framework 源代码 · Ink lindexi 2019-08-31 16:55:58 +0800 ...
- 浅谈vue $mount()
Vue 的$mount()为手动挂载,在项目中可用于延时挂载(例如在挂载之前要进行一些其他操作.判断等),之后要手动挂载上.new Vue时,el和$mount并没有本质上的不同. 具体见代码: 顺便 ...