01 backpack problems [Dynamic Planning]

Source: Internet
Author: User

Problem:

Assume that there are n items, each item has weight, and each item is also valuable. To put these items in a backpack, the carrying capacity of this backpack is limited, how can we maximize the total value of items in a backpack?


Symbol:

N: number of items

W: carrying weight of a backpack

W [I]: Weight of item I (1 <I <= N)

V [I]: Value of item I (1 <I <= N)

C [I, j]: the optimal weight limit of the backpack to item I is J (1 <I <= N, 1 <j <= W)


Analysis:

  1. Optimal Solution Structure: For item I, there are only two conditions: Put or not put. Assuming that item I is placed in a part of the optimal solution, then we can remove item I, C [I-1, J-W [I] at this time should also be the optimal solution
  2. Recursively defines the value of the optimal solution:
  3. Calculate the optimal value based on the bottom-up mode: double loop, I from 1 to n loop, J from 1 to W Loop
  4. An optimal solution is constructed from the calculated results: top-down, I loops from N to 1, if C [I, j]> C [I-1, J], put item I into the backpack; otherwise, the opposite is true.
Code (C ++ ):
#ifndef ___1package__package__#define ___1package__package__#include <iostream>#include <string>#include <vector>using namespace std;class Package{public:    void init(string dataSource);    void print();private:    void compute();    int m_nN;  // The number of object    int m_nW;  // The max weight of package    vector<int> m_vecWeight;  // The weight of object    vector<int> m_vecValue;   // The value of object    vector<vector<int> > m_vecMatrix;  // The results matrix    };#endif /* defined(___1package__package__) */

////  package.cpp//  01package#include "package.h"#include <fstream>void Package::init(string dataSource){    // read data from datasource    ifstream file;    file.open(dataSource);    if (file.is_open()){        string buffer;        file>>m_nW;        file>>m_nN;        m_vecWeight.clear();        m_vecWeight.push_back(0);        m_vecValue.clear();        m_vecValue.push_back(0);        int w, v;        for (int i=0; i<m_nN; ++i) {            file>>w>>v;            m_vecWeight.push_back(w);            m_vecValue.push_back(v);        }        while(!file.eof()){            file>>buffer;            cout<<buffer<<endl;        }    }    file.close();        m_vecMatrix.clear();    // set first row and first column to zero    for (int i=0; i<=m_nN; ++i) {        m_vecMatrix.push_back(vector<int>(m_nW+1, 0));    }    }void Package::print(){    compute();    cout<<"\n=====The Results======\n"<<"The max value: "<<m_vecMatrix[m_nN][m_nW]<<endl;}void Package::compute(){    for (int i=1; i<=m_nN; ++i) {        for (int j=1; j<=m_nW; ++j) {            int cur_weight = m_vecWeight[i];            if (cur_weight>j) {                m_vecMatrix[i][j] = m_vecMatrix[i-1][j];            } else{                m_vecMatrix[i][j] = max<int>(m_vecMatrix[i-1][j-cur_weight]+m_vecValue[i], m_vecMatrix[i-1][j]);            }        }    }}

The input test data can refer to: http://zhidao.baidu.com/question/77646243.html

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.