当前位置:网站首页>[niuke.com] DP30 [template] 01 Backpack
[niuke.com] DP30 [template] 01 Backpack
2022-06-11 21:48:00 【When the flower does not wither】

The output of the first line is typical 0-1 knapsack problem , And the second line is that the volume is required to be fixed with the maximum value , Therefore, we need to dp2 Initialize to int The minimum value that a type can express , And will dp2[ 0 ] The assignment is 0.
#include<iostream>
#include<cstdio>
#include<climits>
using namespace std;
const int N = 1000 + 10;
int w[N], v[N], dp1[N], dp2[N];
int main(){
int n, m;
while(scanf("%d%d", &n, &m) != EOF){
for(int i = 0; i < n; i++) scanf("%d%d", &w[i], &v[i]);
fill(dp2, dp2 + m + 1, -INT_MAX);
dp2[0] = 0;
for(int i = 0; i < n; i++){
for(int j = m; j >= w[i]; j--){
dp1[j] = max(dp1[j], dp1[j - w[i]] + v[i]);
dp2[j] = max(dp2[j], dp2[j - w[i]] + v[i]);
}
}
printf("%d\n", dp1[m]);
if(dp2[m] < 0) printf("0\n");
else printf("%d\n", dp2[m]);
}
return 0;
}
边栏推荐
猜你喜欢

LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)

Codeworks round 744 (Div. 3) problem solving Report

189. rotation array

Why is rpa+ low code a powerful tool to accelerate the digital transformation of finance?

学习位段(1)

Classes and objects (2)

联调这夜,我把同事打了...

Cdr2022 serial number coreldraw2022 green key

Usage of esp32c3 Arduino Library

Nmap进行主机探测出现网段IP全部存活情况分析
随机推荐
LeetCode-104-二叉树的最大深度
JVM|运行时数据区;程序计数器(PC寄存器);
206.反转链表
189. rotation array
实现栈和队列
189. 轮转数组
EndnoteX9简介及基本教程使用说明
Educational Codeforces Round 114 (Rated for Div. 2) D
JVM | virtual machine stack (local variable table; operand stack; dynamic link; method binding mechanism; method call; method return address)
类和对象(4)
Refresh and upgrade | innovation, starting from cloud store
Cdr2022 serial number coreldraw2022 green key
网络连接正常但百度网页打不开显示无法访问此网站解决方案
Classes and objects (4)
Experiment 10 Bezier curve generation - experiment improvement - control point generation of B-spline curve
JVM|前言介绍
Redis basic data type (set)
Codeworks round 740 Div. 2 problem solving Report
The same efficiency tool for leading enterprises to promote smart finance. Let's have a quick look?
C语言实现八种排序(3)