0-1 knapsack problem

Source: Internet
Author: User

Problem:

There are n items and a backpack with a capacity of V. The value of article I is c[i] and the weight is w[i]. The solution of which items are loaded into the backpack makes these items less than the total weight of the backpack and the maximum value.

The problem is characterized by the following: Each item has only one piece, can choose to put or not to put. Use f[i][j] to indicate that the backpack current capacity is J, choose the maximum value when loading 1-i items

In the knapsack problem of finding the optimal solution, there are two kinds of different questions: 1, the optimal solution that is less than or equal to the knapsack capacity, that is not necessarily full of backpacks, 2, the request "just fill the backpack" when the optimal solution.

1, the optimal solution is less than or equal to the capacity of the backpack, that is not necessarily filled with backpacks;

Set f[0..n][0..v]=0

2. The optimal solution is required for "just fill the backpack".

F[0][1..v]=-∞ to ensure that the best solution is not available when the backpack is dissatisfied (maximum value)

State transition equation:

    1. F[I][J]=F[I-1][J] J<w[i-1]
    2. F[I][J]=MAX{F[I-1][J].F[I-1][J-W[I-1]]+C[I-1]} Other

Explain:

1. Backpack current capacity j is less than the weight of article I items w[i-1], the article I is not fit, all do not put the article I items

2. Items I can put down, choose, put or not put

    • If not, the problem translates to "top I-1 items in a backpack with a capacity of J"
    • If put, then the problem translates to "the first I-1 items into the remaining capacity of j-w[i-1] in the backpack", at this time can obtain the greatest value is f[i-1][j-w[i-1]], plus the value obtained by placing the article I item c[i-1]

In the "Other" case, the value of f[i][j] is the maximum value in both cases

Code:

1#include <iostream>2 using namespacestd;3 intf[ -][ -];4 intmin=-999999;5 intPackageintNintVintW[],intC[])//N-Item quantity, V-pack capacity, w[] weight of each item, c[] The value of the corresponding item6 {7     if(f[n][v]!=0)//The sub problem has been calculated8         returnF[n][v];9     if(n==0|| v<=0)//Question number One: The value is the biggest, does not require fillTen         return 0; One  A     //if (n==0)//Question number Two: The maximum value when the backpack is full, set the f[0][0]=0,f[0][1..v]=-∞, that is, when the backpack is dissatisfied with no optimal solution -     //{ -     //if (v!=0) return Min; the     //return 0; -     //} -  -     if(v<w[n-1])//The weight of the nth item is greater than the capacity of the current backpack, then the nth item is not loaded, the backpack capacity is still V, and the remaining n-1 items are selected +F[n][v]=package (n1, v,w,c); -     Else               //backpack Current capacity can be installed under the nth item, you can choose to install or do not install nth items +     { A         intA=package (n1, v,w,c);//do not install nth items, choose from the remaining n-1 items, the backpack capacity is still v at         intB=package (n1, v-w[n-1],W,C) +c[n-1];//n items, the current value is the value of the nth item C[n] Plus (n-1,v-w[n]) The optimal solution of the problem -f[n][v]=a>b?a:b;//Choose the solution with the greatest value -     } -     returnF[n][v]; - } -  in intMain () - { toMemset (F,0,sizeof(f));//set Boundary conditions +     intn,v,w[ -],c[ -]; -cout<<"Input the number of items:"; theCin>>N; *cout<<"input the volume of items:"; $Cin>>v;Panax Notoginsengcout<<"input the weight of items:"; -      for(intI=0; i<n;i++) theCin>>W[i]; +cout<<"input the value of items:"; A      for(intI=0; i<n;i++) theCin>>C[i]; +cout<<"The bigest value is:"; -Cout<<package (n,v,w,c) <<Endl; $}
View Code

Results:

Ask law one: to find the optimal solution is less than equal to the capacity of the backpack, that is not necessarily full pack;

Ask law two: the optimal solution is required to "just fill the backpack".

0-1 knapsack problem

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.