当前位置:网站首页>动态规划多重背包问题(二进制优化)
动态规划多重背包问题(二进制优化)
2022-07-21 08:02:00 【战士小小白】
多重背包问题(二进制优化)
有 N 种物品和一个容量是 V 的背包。
第 i 种物品最多有 si 件,每件体积是 vi,价值是 wi。
求解将哪些物品装入背包,可使物品体积总和不超过背包容量,且价值总和最大。
输出最大价值。输入格式
第一行两个整数,N,V,V,用空格隔开,分别表示物品种数和背包容积。
接下来有 N 行,每行三个整数 vi,wi,si,用空格隔开,分别表示第 i 种物品的体积、价值和数量。
输出格式
输出一个整数,表示最大价值。
数据范围
0<N≤10000<N≤1000
0<V≤20000<V≤2000
0<vi,wi,si≤20000<vi,wi,si≤2000提示:
本题考查多重背包的二进制优化方法。
输入样例
4 5 1 2 3 2 4 1 3 4 3 4 5 2
输出样例:
10
#include <bits/stdc++.h>
using namespace std;
const int N = 12010, M = 2010;
int n, m;
int f[M];
int v[N], w[N];
int main()
{
scanf("%d%d", &n, &m);
int cnt = 0;
for(int i = 1; i <= n; i ++)
{
int a, b, s;
scanf("%d%d%d", &a, &b, &s);
int k = 1;
while(k <= s)
{
cnt ++;
v[cnt] = k * a;
w[cnt] = k * b;
s -= k;
k *= 2;
}
if(s > 0)
{
cnt ++;
v[cnt] = s * a;
w[cnt] = s * b;
}
}
n = cnt;
for(int i = 1; i <= n; i ++)
for(int j = m; j >= v[i]; j --)
f[j] = max(f[j], f[j - v[i]] + w[i]);
printf("%d",f[m]);
return 0;
}
边栏推荐
- Uniapp, wechat applet input regular verification can only be input as numbers and decimal places
- Framework API在线查看源码
- 每个博主都需要问自己的7个基本问题
- weirdo The interview topics include toilet habits, eating time and sleeping time
- 零基础转行软件测试学习要不要报培训班学习,还是自学好?
- Kaptcha验证码的配置与使用
- A 股指数历史数据 API 数据接口
- 转载:CAN总线终端电阻
- c语言:查漏补缺(一)
- Hardcore Fiddler packet capturing tool large strategy (end) Fiddler ultimate
猜你喜欢
C语言经典一百题(1-10题)(含答案)
windows10上安装mysql 5.7.37
C language: leak detection and filling (I)
[dongle vulnerability notification] Apache spark command injection vulnerability scheme
“我放弃编程,写了一本130万字的硬科幻小说”
Jupyterhub配置Go环境
什么是深克隆,浅克隆?(案例详解)
Spa single page learning and understanding
联想小新air13 pro重装win10时出现找不到存储设备驱动
A 股分笔交易数据 API 数据接口
随机推荐
Learn the necessary tools of automation selenium think about automated testing in the pit again
【安全狗漏洞通告】Apache Spark 命令注入漏洞方案
时间不一致
学习路之PHP--thinkphp5+windows服务器实现定时任务
c语言进阶篇:数据的存储(深度剖析-整型)
46: Chapter 4: develop file services: 7: integrate Alibaba OSS in [files] file services;
[wechat applet] solve the problem that the code upload exceeds the size limit and the applet is subcontracted
C language: leak detection and filling (I)
weirdo The interview topics include toilet habits, eating time and sleeping time
好用的办公网优化工具OneDNS
Mysql03(关联查询)
重大突破!首款国产科学计算软件研发成功
【集训DAY9】Maze【线段树】
NetworkX的基本用法
JS regular case
屏幕共享软件--Deskreen
Database connection pool
罗丹明B标记肽核酸PNA|罗丹明B-PNA|生物素修饰肽核酸pna|生物素修饰pna肽核酸|规格信息
Ala-PNA丙氨酸改性PNA肽核酸|Ac-Ala-PNA的合成路线
I.MX6U-ALPHA开发板(蜂鸣器实验)