POJ 3624 Charm bracelet knapsack Problem Solution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.