当前位置:网站首页>Dynamic programming knapsack problem -- 01 knapsack
Dynamic programming knapsack problem -- 01 knapsack
2022-07-22 01:32:00 【wwwwza】
2022.7.20
Summary of the theme : Yes n Items And a Capacity of p The backpack , Each item has weight w and value v Two properties , It is required to select several items to put into the backpack, so that the total value of the items in the backpack is the largest, and the total weight of the items in the backpack does not exceed the capacity of the backpack .
In the above example , Because each object has only two possible states ( Take or not ), Corresponds to... In binary 0 and 1, This kind of problem is called 「0-1 knapsack problem 」.
We use two-dimensional arrays dp[i][j] It means to put it in front only i When items , The capacity of the backpack does not exceed j The greatest value of time . Dynamic transfer equations can be listed dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]).
for(int i=1;i<=n;i++){
for(int j=p;j>=w[i];j--){
dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);
}
}
But open dp[n][p] Your space will MLE, So we're going to use a scrolling array , Get the dynamic transfer equation , Delete one dimension , Keep right dp[i][j] Influential i( Quantity of articles )dp[j]=max(dp[j],dp[j-w[i]]+v[i]).
front i-1 Items | The first i Items |
---|---|
j-w[i] | v[i] |
for(int i=1;i<=n;i++){
for(int j=p;j>=w[i];j--){
f[j]=max(f[j],f[j-w[i]]+v[i]);
}
}
Be careful , We traverse from back to front , Because for the currently processed items i And the current state dp[i][j], stay j>=w[i] when ,dp[i][j] Yes, it will be dp[i][j-w[i]] Affected by . This is equivalent to items It can be put into the backpack many times , It doesn't agree with the question . To avoid that , We can change the order of enumeration , from p Enumerate to w[i], In this way, the above errors will not occur , because dp[i][j] Always in dp[i][j-w[i]] Updated before .
Real exercises :( Information Science Olympiad all in one 1267:【 example 9.11】01 knapsack problem )
Title Description :
A traveler has one that can hold up to MM Kilogram backpack , Now there is n Item , Their weights are W1,W2,...,Wn, Their values are C1,C2,...,Cn, Ask travelers to get the maximum total value .
Input format :
first line : Two integers ,M( Backpack Capacity ,M<=200) and N( Quantity of articles ,N<=30); The first 2..N+1 That's ok : Two integers per line Wi,Ci, Indicates the weight and value of each item .
sample input :
10 4
2 1
3 3
4 5
7 9
sample output :
12
Ideas :
This is a template question .
Code :
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <list>
#include <map>
#define int long long
using namespace std;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int p,n,w[40],c[40],f[400];
signed main(){
cin >>p>>n;
for(int i=1;i<=n;i++){
cin >>w[i]>>c[i];
}
for(int i=1;i<=n;i++){
for(int j=p;j>=w[i];j--){
f[j]=max(f[j],f[j-w[i]]+c[i]);
}
}
cout <<f[p];
return 0;
}
边栏推荐
- 奇葩!面试题目竟设置如厕习惯、吃饭时长、入睡时间
- Check the version number of IAR project
- 45: Chapter 4: developing file services: 6: third party cloud storage solutions [Alibaba cloud OSS]; (purchase OSS services; subscribe to services; create a bucket;)
- [Yugong series] go teaching course in July 2022 014 arithmetic operators of operators
- Hardcore Fiddler packet capturing tool large strategy (end) Fiddler ultimate
- 小程序 奇葩BUG 更新~
- 转载:CAN总线终端电阻
- 如何修改MySQL监听IP地址
- 在go语言中获取当前时间,以及md5、hmac、sha1算法的简单实现
- Mysql08 (transaction)
猜你喜欢
NetworkX的基本用法
叠氮化物标记pna肽核酸|亚甲基蓝标记pna肽核酸|酪氨酸修饰肽核酸PNA|Tyr-PNA齐岳bio
Basic usage of Networkx
【集训DAY8】Tent【数学】【DP】
近红外染料CY7.5标记PNA多肽实验步骤CY7.5-PNA|188Re标记反基因肽核酸(AGPNA)
SPA单页面学习理解
可视化:这十个数据可视化工具软件平台你必须知道
开源demo| ARCall 小程序开源示例发布
46: Chapter 4: develop file services: 7: integrate Alibaba OSS in [files] file services;
First knowledge of loop and branch statements in C language
随机推荐
数据库事务隔离级别
硬核Fiddler抓包工具大型攻略(完)Fiddler终极篇
04-1、默认成员函数:构造函数、析构函数
Malloc and space Configurator
Appium脚本启动参数如何设置
05-1、默认成员函数:拷贝构造函数、赋值运算符重载
【集训DAY9】Light Tank【动态规划】
token和session的区别,这个经典面试题值得一个更深入的回答
[wechat applet] solve the problem that the code upload exceeds the size limit and the applet is subcontracted
LeetCode 10. Regular Expression Matching
06-1、友元、初始化列表初始化、nei‘bu
Laike can optical transceiver solves the problem of fire networking of linked fire engines such as Fu'an fs5216/fs5116
[Yugong series] go teaching course in July 2022 014 arithmetic operators of operators
Mysql05 (view)
Ala-PNA丙氨酸改性PNA肽核酸|Ac-Ala-PNA的合成路线
Uniapp, wechat applet input regular verification can only be input as numbers and decimal places
(pytorch advanced road VI) implementation of swing transformer
Mysql05(视图)
【集训DAY10】Tree【区间DP】
CRM 概念:了解Leads、Prospect、MQL 和 SQL 的概念