Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the knapsack problem

Shulou Source: shulou.com Published: 2022-06-03 05:37:00 10月03日 Update

This article mainly explains "how to understand the backpack problem," interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to understand the backpack problem"!

0-1 Two-dimensional bc of knapsack problem

#include #include #include using namespace std;const int N=1010;int n,m;int v[N],w[N];int f[N][N]; //valency sum int main(){ cin>>n>>m; for(int i=1;i>v[i]>>w[i]; //When 0 items are selected, the sum of values is 0, i.e. f[0][0~m] = 0; //Since it has been defined as a global variable, it has been initialized to 0, so it can not be written. for(int i=1;iv[i]>>w[i]; for(int i=1;i=v[i];j--) f[j]=max(f[j],f[j-v[i]]+w[i]); cout

Tags: Backpacks questions values content summation learning practicality deeper global interests variables practicality practicality simplicity of operation arrays methods more friends items websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia vpn Shulou Information Huawei macOS