# #算法
Bare 01 backpack.
# #分析
This is the most basic knapsack problem, characterized by: only one piece of each item, you can choose to put or not put.
Define a state with a sub-question: F[i][v] Indicates the maximum value that the first I item (partial or total) fits into a backpack with a capacity of V. The state transfer equation is: F[i][v]=max{f[i-1][v],f[i-1][v-w[i]]+c[i]}.
This equation is very important, and basically all the equations for knapsack-related problems are derived from it. So it is necessary to explain it in detail: "Put the first I item into a backpack with a capacity of V" This sub-problem, if only consider the article I item strategy (put or not), then can be converted into a only 1 items of the first I-item problem. If you do not put the article I item, then the problem is converted to "the first I-1 items into the capacity of the backpack"; If you put the article I, then the problem is converted to "the first I-1 items into the remaining capacity of v-w[i] backpack", the maximum value can be obtained is F [i-1][v-w[i]] Plus the value obtained by placing the article I item c[i].
The time and space complexity of the above methods are O (N\*v), in which the time complexity can no longer be optimized, but the spatial complexity is optimized to O (V).
First consider how to achieve the basic idea above, there must be a main loop I=1..N, each time the two-dimensional array f[i][0..v] all values. So, if you only use an array f [0..V], can you guarantee that the f[v] in the end of the first cycle is the state we defined F[I][V]? F[I][V] (F[i-1][v] and F[i-1][v-w[i]] Two sub-problems are recursive, can you guarantee to push the f[i][v] (also in the main cycle of the first time) can get f[v] and F[i-1][v]] "value?" In fact, this requires us to v=v in each main loop. 0 of the reverse push f[v], so as to ensure that push f[v] f[v-w[i]] saved is the value of state F[i-1][v-w[i]].
The pseudo code is as follows:
For I=1..N
For V=v. 0
F[v]=max{f[v],f[v-w[i]]+c[i]};
where F[v]=max{f[v],f[v-w[i]]+c[i]} is equivalent to the transfer equation F[i][v]=max{f[i-1][v],f[i-1][v-w[i]]+c[i]}, because now F[v-w[i]] is equivalent to the original f[i-1][ V-w[i]]. If the cycle order of V is changed from the reverse order of the above, then it becomes the f[i][v] [f[i][v-w[i]] infer, and this test instructions, but it is another important complete knapsack problem The simplest solution, so learning to use only one-dimensional array to solve 01 knapsack problem is very necessary.
In simple terms, the f[i][v] represents the first I item, the total weight does not exceed the optimal value of V, then F[i][v]=max (F[i-1][v-w[i]]+c[i],f[i-1][v]); F[n][m] is the optimal solution.
# #程序
# # #NO. 1:
"' CPP
#include <cstdio>
using namespace Std;
const int MAXM = 201, maxn = 31;
int m, n;
int W[MAXN], C[MAXN];
int F[MAXN][MAXM];
int max (int x,int y) {x>y?x:y;}//Find x and y maximum
int main () {
scanf ("%d%d", &m, &n); Backpack capacity m and number of items n
for (int i = 1; I <= n; i++)//In initializing the loop variable section, define a variable and initialize
scanf ("%d%d", &w[i],&c[i]); Weight and value of each item
for (int i = 1; I <= n; i++)//F[I][V] represents the first item, the total weight does not exceed the best value of V
for (int v = m; v > 0; v--)
if (W[i] <= v) f[i][v] = max (f[i-1][v],f[i-1][v-w[i]]+c[i]);
else f[i][v] = f[i-1][v];
printf ("%d", f[n][m]); F[N][M] for the optimal solution
return 0;
}
```
It is convenient to use a two-dimensional array to store each sub-problem, but when the MAXM is large, such as maxm=2000 cannot define a two-dimensional array F, what to do, in fact, you can use a one-dimensional array.
The mathematical model of this problem is as follows: setting F[v] means that the weight does not exceed the maximum value of v kg, then f[v]=max{f[v],f[v-w[i]]+c[i]}, when v>=w[i],1<=i<=n.
# # #NO. 2:
"' CPP
#include <cstdio>
using namespace Std;
const int MAXM = 2001, MAXN = 31;
int m, n;
int W[MAXN], C[MAXN];
int F[MAXM];
int main () {
scanf ("%d%d", &m, &n); Backpack capacity m and number of items n
for (int i=1; i <= n; i++)
scanf ("%d%d", &w[i],&c[i]); Weight and value of each item
for (int i=1; i <= n; i++)//Set F (v) to indicate the maximum value of the weight not exceeding v kg
for (int v = m; v >= w[i]; v--)
if (F[v-w[i]]+c[i]>f[v])
F[V] = F[v-w[i]]+c[i];
printf ("%d", f[m]); F (m) is the optimal solution
return 0;
}
```
# # #NO. 3:
"' CPP
#include <iostream>
#include <cassert>
using namespace Std;
int tt[200];
int vv[200];
int dp[1005][105];
int main () {
int T, M;
CIN >> T >> M;
ASSERT (1 <= t && t <= 1000);
ASSERT (1 <= m && m <= 100);
int I, J;
for (i = 1; I <= M; i++) {
CIN >> Tt[i] >> vv[i];
ASSERT (1 <= tt[i] && tt[i] <= 100);
ASSERT (1 <= vv[i] && vv[i] <= 100);
}
for (i = 1; I <= T; i++) {
for (j = 1; J <= M; j + +) {
int a = dp[i][j-1];
if (i >= tt[j]) {
int B = dp[i-tt[j]][j-1] + vv[j];
if (b > a) a = B;
}
DP[I][J] = A;
}
}
cout << dp[t][m] << Endl;
{char C;assert (!) ( CIN >> C));}
return 0;
}
```
# #总结
01 knapsack problem is the most basic knapsack problem, it contains the knapsack problem design state, the basic idea of the equation, in addition, other types of knapsack problem can also be converted to 01 knapsack problem solving. Therefore, we must carefully understand the above basic idea of the method, the significance of the state transfer equation, and finally how to optimize the space complexity.
C + + drug extraction