当前位置:网站首页>0-1背包问题
0-1背包问题
2022-07-27 22:24:00 【hnjzsyjyj】
【题目来源】
https://www.acwing.com/problem/content/description/2/
【题目描述】
有 N 件物品和一个容量是 V 的背包。每件物品只能使用一次。
第 i 件物品的体积是 vi,价值是 wi。
求解将哪些物品装入背包,可使这些物品的总体积不超过背包容量,且总价值最大。
输出最大价值。
【输入格式】
第一行两个整数,N,V,用空格隔开,分别表示物品数量和背包容积。
接下来有 N 行,每行两个整数 vi,wi,用空格隔开,分别表示第 i 件物品的体积和价值。
【输出格式】
输出一个整数,表示最大价值。
【数据范围】
0<N,V≤1000
0<vi,wi≤1000
【算法代码一】
#include <bits/stdc++.h>
using namespace std;
const int maxn=1005;
int v[maxn]; //volume
int w[maxn]; //worth
int f[maxn][maxn]; //f[i][j], the maximum value of the previous i items under j volume
int main() {
int n,m;
cin>>n>>m;
for(int i=1; i<=n; i++)
cin>>v[i]>>w[i];
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++) {
//If the current backpack can't hold the i-th item, the value is equal to the previous i-1 item
if(j<v[i]) f[i][j]=f[i-1][j];
//If yes, the decision is made whether to select item i
else f[i][j]=max(f[i-1][j],f[i-1][j-v[i]]+w[i]);
}
cout<<f[n][m]<<endl;
return 0;
}
/*
in:
4 5
1 2
2 4
3 4
4 5
out:
8
*/
【算法代码二(一维数组优化)】
#include<bits/stdc++.h>
using namespace std;
const int maxn=1005;
int f[maxn];
int main() {
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
int v,w;
cin>>v>>w;
for(int j=m;j>=v;j--)
f[j]=max(f[j],f[j-v]+w);
}
cout<<f[m]<<endl;
return 0;
}
/*
in:
4 5
1 2
2 4
3 4
4 5
out:
8
*/
【参考文献】
https://www.acwing.com/solution/content/1374/
边栏推荐
- 为华为打造无美系设备的产线,台积电三星能做到吗?
- Rancher2.6 monitoring grafana docking LDAP
- Network equipment hard core technology insider firewall and security gateway (V) security double repair method
- 推荐系统-模型:wide&deep 模型
- 网络设备硬核技术内幕 防火墙与安全网关篇 (九) 虚拟化神器 (下)
- Ink wheel salon | Li Wenjie, Peking University: a graph database system for knowledge atlas application gstore
- 【C语言入门】ZZULIOJ 1026-1030
- 特权更改对现有连接的影响
- Set 数据构造函数
- 推荐系统-模型:dssm双塔模型做embedding的召回
猜你喜欢

多线程及多线程程序的编写

Fastjson历史漏洞复现

Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm

Build Release Blogs

How to smoothly go online after MySQL table splitting?

Postman download and use tutorial

SRv6初登场

In July, a software testing engineer came to the company. He looked like a hairy boy. He didn't expect to be the new generation of roll King

Point divide and conquer analysis

激活最大化
随机推荐
网络设备硬核技术内幕 防火墙与安全网关篇 (八) 虚拟化神器 (中)
蓝桥杯单片机第十一届国赛程序设计试题
Network equipment hard core technology insider firewall and security gateway (12) the mystery of zero contact office
Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!
立即报名 | 云原生技术交流 Meetup 广州站已开启,8 月 6 号与你相遇!
Add a picture in front of the cell
Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm
What is the org relationship mitigation strategy of Microsoft edge browser tracking prevention
LSB steganography
Interesting Huffman tree
2020年一季度可穿戴市场出货量达7260万部,苹果独占近三成市场份额
推荐系统-指标:ctr、cvr
[bre] software build release automation
Red team killer behinder_ V4.0 (ice scorpion 4.0)
The server is poisoned - the dish is the original sin
Srv6 debut
Function related knowledge
Network equipment hard core technology insider firewall and security gateway chapter (VI) security double repair under the law
R language evaluates the relative importance of the predictive factors (variables, characteristics) of the regression model, scales the predictive variables of the regression model, and then construct
多线程及多线程程序的编写