[Algorithm Review 2] problem summary

Source: Internet
Author: User
I. Problem description:

There are n items and a backpack with a capacity of v. The cost of the I-th item is C [I], and the value is W [I]. Solving which items are loaded into a backpack can maximize the total value.
The so-called 01 backpack indicates that each item has only one, either loaded or not loaded.

Ii. solution:
Consider using the DP to solve the problem. Define a recursive OPT [I] [v] to indicate the first I items. When the size of the backpack is V, the maximum load is allowed.
OPT [I] [v] = max (OPT [I-1] [v], OPT [I-1] [V-C [I] + W [I])
Explanation:
OPT [I-1] [v] indicates the I-th item is not packed into a backpack, OPT [I-1] [V-C [I] + W [I] indicates that the I-th item is loaded into the backpack.

The cost is as follows:
The time complexity is O (V * T), and the space complexity is O (V * t ). Time complexity cannot be optimized, but space complexity can be optimized.
However, the V descent method must be traversed, that is, the V... 0 method.

3. Initialization:
(1) If it is required that the backpack be full, it is initially as follows:
F [0] = 0, F [1 .. v] indicates-INF. Indicates that when the volume is 0, only one item with the volume of 0 is accepted into the package.
(2) If the backpack is required to be empty, the initialization is as follows:
F [0... V] = 0, indicating that all backpacks of any volume have an effective solution that is 0.
The specific explanation is as follows:
The initialized F array is actually the legal status when no item can be put into a backpack.
If a backpack is required to be filled, only a backpack with a capacity of 0 may be filled with a value of 0 ",
There is no legal solution for backpacks of other capacities. They are undefined and their values should be-∞.
If a backpack does not have to be filled up, there is a legal solution for a backpack of any capacity: "Nothing is loaded ",
The value of this solution is 0, so the initial state value is all 0.

The Code is as follows:

/**//*
01 backpack, using the optimized storage space
Create an array

F [I] [v] = max (F [I-1] [v], F [I-1] [V-C [I] + W [I])
Place the first I item into the maximum value of the backpack with a capacity of v.


The following describes an optimization, represented by a one-dimensional array.
(1) f [v] indicates the maximum value for each type of item with a capacity of v.
However, when the volume is recycled, it needs to decrease from V ---- 1.


Initialization problems:
(1) If no space is allowed in the backpack, you can initialize f [0] to 0, and initialize other f [1. N] To-INF.
Indicates that items with a mass of 0 are allowed only when the volume is 0.
When the volume is not 0, items with a mass of 0 cannot be placed, and the status is set to unknown.



(2) If the backpack is required to have sufficient space, you can initialize f [1N] to 0.
In this way, you can leave it empty when it cannot be placed.




*/

# Include <iostream>
Usingnamespace STD;
Constint v = 1000
; // Total volume

Constint T = 5
; // Item type

Int f [V + 1]
;
// # Define empty // it can be left empty
Int
W [T] = {8
, 10,
4, 5, 5}; // Value
Int
C [T] = {1, 600
, 400,
200,200,300}; // each volume
Constint INF =-66536
;

Int package ()
{
# Ifdef empty
For (INT I = 0
; I <= V; I ++) // Conditional compilation, indicating that the backpack can not be fully stored
F [I] = 0;
# Else
F [0] = 0
;
For (INT I = 1
; I <= V; I ++) // Conditional compilation, indicating that all the backpacks must be fully stored
F [I] = inf;
# Endif

For (INT I = 0
; I <t; I ++)
{
For (INT v =
V; V> = C [I]; V --) // All values must be decreased from V to 0.
{
F [v] = max (F [V-C [I] +
W [I], F [v]); // This f [v] essentially represents the value before the I-1 times.
}
}
Return f [v];
}

Int main ()
{

Int temp = package ();
Cout <temp <Endl;
System ("pause ");
Return0;
}

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.