当前位置:网站首页>[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;
}
边栏推荐
猜你喜欢
随机推荐
联调这夜,我把同事打了...
How to use the transaction code sat to find the name of the background storage database table corresponding to a sapgui screen field
LeetCode-98-验证二叉搜索树
自定义实现offsetof
189. rotation array
LeetCode-43-字符串相乘
建造者模式
快速排序的非递归写法
继承的所有特征
Carry and walk with you. Have you ever seen a "palm sized" weather station?
Leetcode-129- sum of numbers from root node to leaf node
C语言实现八种排序(1)
Deploy SAP ui5 applications to the sap BTP kyma operating environment step by step
不使用加减乘除做加法
每日一题-删除有序数组的重复项
JVM|虚拟机栈(局部变量表;操作数栈;动态链接;方法的绑定机制;方法的调用;方法返回地址)
Some error reporting assemblies of cann code
servlet获取表单数据
Classes and objects (4)
C语言实现八种排序 - 归并排序








