01背包
最后更新于:2022-04-01 20:30:59
~~~
void dp(int *w, int *v, int n, int c){
int **m;
m = new int*[n+1];
for (int i = 0; i= 2; i--){
for (int j = c; j >= 1; j--){
if (j - w[i - 1] >= 0){
int tmp = m[i + 1][j - w[i - 1]] + v[i - 1];
m[i][j] = m[i + 1][j]>tmp ? m[i + 1][j] : tmp;
}
else{
m[i][j] = m[i + 1][j];
}
}
}
if (c >= w[0]) m[1][c] = m[2][c - w[0]] + v[0];
else m[1][c] = m[2][c];
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= c; j++) cout << m[i][j] << "\t";
// cout << endl;
// }
int *x;
x = new int[n+1];
for (int i = 0; i
';