0-1 backpack

Source: Internet
Author: User

0-1 backpack

Problem:

There are N items and a backpack with a capacity of V. The value of item I is c [I], and the weight is w [I]. Solving which items are loaded into a backpack can make the total weight of these items not exceed the size of the backpack, and the total value is the largest.

This problem is characterized by: each item has only one item, and you can choose to put it or not. F [I] [j] indicates that the current size of the backpack is j, and the maximum value of 1-I items is selected.

There are two different ways to solve the problem of a backpack: 1. Find the optimal solution smaller than or equal to the capacity of the backpack, that is, it is not necessarily filled with a backpack; 2. The optimal solution should be "just filled with a backpack.

1. Find the optimal solution that is smaller than or equal to the capacity of the backpack, that is, not necessarily full of the backpack;

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

2. The optimal solution should be "just filled with a backpack.

Then f [0] [1. v] =-∞ ensures that the optimal solution is not obtained when the backpack is not satisfied (the greatest value)

State transition equation:

Explanation:

1. the current size of the backpack j is less than the weight of the I items w [I-1], I items cannot be loaded, all do not put I items

2. items on the I-th item can be put down, selected, put or not put

  • If not, the problem is converted to "pre-i-1 items are placed in a backpack with capacity of j"
  • If put, the problem is converted to "putting the items in the front I-1 into the backpack with the remaining capacity j-w [I-1 ", the greatest value that can be gained at this time is f [I-1] [j-w [I-1] plus the value gained by placing the I item c [I-1]

In the case of "others", the value of f [I] [j] is the maximum value in the preceding two cases.

Code:

1 # include <iostream> 2 using namespace std; 3 int f [100] [100]; 4 int Min =-999999; 5 int package (int n, int v, int w [], int c []) // n-item quantity, v-backpack capacity, w [] weight of each item, c [] value of the corresponding item 6 {7 if (f [n] [v]! = 0) // This subproblem has been calculated. 8 return f [n] [v]; 9 if (n = 0 | v <= 0) // question type 1: maximum Value: 10 return 0; 11 12 // if (n = 0) // question type 2: maximum value when a backpack is full, set f [0] [0] = 0, f [0] [1 .. v] =-∞, that is, when the backpack is not satisfied, there is no optimal solution 13 // {14 // if (v! = 0) return Min; 15 // return 0; 16 //} 17 18 if (v <w [n-1]) // if the weight of the n-th item is greater than the current size of the backpack, the n-th item is not loaded. The size of the backpack is still v, select 19 f [n] [v] = package (n-1, v, w, c) from the remaining n-1 items ); 20 else // the current size of the backpack can hold the nth item. You can choose to hold or not hold the nth item 21 {22 int a = package (n-1, v, w, c ); // if n items are not loaded, select from the remaining n-1 items. The size of the backpack is still v23 int B = package (n-1, v-w [n-1], w, c) + c [n-1]; // installs the nth item, and the current value is the value of the nth item c [n] Plus (n-1, v-w [n]) optimal Solution 24 f [n] [v] = a> B? A: B; // select the most valuable solution 25} 26 return f [n] [v]; 27} 28 29 int main () 30 {31 memset (f, 0, sizeof (f); // set the boundary condition 32 int n, v, w [100], c [100]; 33 cout <"input the number of items :"; 34 cin> n; 35 cout <"input the volume of items:"; 36 cin> v; 37 cout <"input the weight of items :"; 38 for (int I = 0; I <n; I ++) 39 cin> w [I]; 40 cout <"input the value of items :"; 41 for (int I = 0; I <n; I ++) 42 cin> c [I]; 43 cout <"the bigest value is :"; 44 cout <package (n, v, w, c) <endl; 45}View Code

Result:

Question 1: Find the optimal solution that is less than or equal to the capacity of the backpack, that is, not necessarily full of the backpack;

Question 2: the optimal solution is required when "just filled with a backpack.

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.