How to solve multiple knapsack problems in C language dynamic programming
This article mainly introduces the relevant knowledge of C language dynamic planning how to solve a variety of knapsack problems, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that after reading this article on how to solve a variety of knapsack problems in C language dynamic planning, we will gain something. Let's take a look.
01 knapsack problem
Detailed explanation of C language Mathematical problem and simple DP01 knapsack problem
Recall this picture first.
Here I will send the 01 knapsack problem code again, which can be used for comparison.
2D:
# includeusing namespace std;const int MAXN = 1005 int int v [MAXN]; / / Volume int w [MAXN]; / / value int f [MAXN] [MAXN]; / / f [I] [j], maximum value of the first I items under j volume int main () {int n, m; cin > n > m; for (int I = 1; I > v [I] > > w [I]; for (int I = 1; I m For (int I = 1; I > v > > w; / input while processing for (int j = m; j > = v; JML -) f [j] = max (f [j], f [j-v] + w);} cout n > > m; for (int I = 1; I > v [I] > w [I]; for (int I = 1; i m; ll aline bc) For (ll iTunes 1 [I] > a > b > > c; for (ll js [I]); for (int juniors j > v [I] [j] > > w [I] [j]); / / read} for (int iridges 1 [iv] [j] > > w [I] [j];}} for (int itimulties 0 / 10 / 0 / 10) {for (int / I / 0 / 1) KMQ -) can also if (j > = v [I] [k]) f [j] = max (f [j], f [JQ v [I] [k]] + w [I] [k]);}} cout