The simplest knapsack problem, the title should be in addition to the Backpack Test Center: You cannot open a two-dimensional array. I haven't opened the two-dimensional. It is impossible to see the data in light.
Too big.
There are two ways to improve the province's memory DP:
1 array of so-called volumes
2 Reverse Table
Long time did not do backpack DP, suddenly think such a knapsack problem is very easy.
Here are two ways to solve the equation:
1 Calbag () is a scrolling array
2 CalBag2 () is reverse form filling
#pragma once#include <stdio.h> #include <stdlib.h> #include <vector>using namespace Std;const int Max_ W = 12881;const int max_n = 3403;int N, m;//m is total weightint wei[max_n];int desi[max_n];int tbl[max_w];int calBag2 () {MEMS ET (tbl, 0, sizeof (int) * (m+1)), for (int i = 1; I <= N; i++) {for (int j = M; J >= Wei[i]; j--) tbl[j] = max (tbl[j), TBL [J-wei[i]]+desi[i]);} return tbl[m];} int calbag () {vector<vector<int> > tbl (2, vector<int> (m+1)), bool id = true;for (int i = 1; I <= N; i++ {for (int j = 1; j < Wei[i] && J <= M; j + +) Tbl[id][j] = tbl[!id][j];for (int j = wei[i]; j <= M; j + +) {T BL[ID][J] = max (Tbl[!id][j], tbl[!id][j-wei[i]]+desi[i]);} Note that all the data in the previous column is pulled down, which is the!id sequence id =!id;} return tbl[!id][m];} int main () {while (scanf ("%d%d", &n, &m)! = EOF) {for (int i = 1; I <= N; i++) {scanf ("%d%d", &wei[i], & Desi[i]);} printf ("%d\n", CalBag2 ());} return 0;}
Copyright notice: The author Heart Jing, Jing Space address: http://blog.csdn.net/kenden23/, may not be reproduced without the consent of the author.
POJ 3624 Charm bracelet knapsack Problem Solution