仅当元素“保存在背包中”时才应打印该元素。在每次迭代中,您都会检查是将元素放入背包还是丢弃它。在您的代码中,您应该检查这一点:如果一个元素“保存在背包中”,请打印它及其重量,以及已经在背包中的其他值,并且在添加此元素时不超过容量背包。有几种方法可以做到这一点。我想到了这个:在执行该方法时,将所选值保存在背包中,为它的每个可能容量(如果它有容量 W,则必须将所选值存储在一个矩阵中,其中每一行 w表示一个值 0
代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <string>
/**
* Responsible for dealing with the unbounded knapsack problem.
*/
class UnboundedKnapsack
{
//-------------------------------------------------------------------------
// Attributes
//-------------------------------------------------------------------------
/**
* Stores maximum value of the knapsack for a certain capacity.
*/
std::vector<int> knapsack;
/**
* Stores elements that are part of the knapsack with a certain capacity.
* <li><b>Line:</b> Knapsack capacity</li>
* <li><b>Column:</b> Elements</li>
*/
std::vector<std::vector<int> > selectedElements;
/**
* Stores maximum knapsack capacity.
*/
int maximumCapacity;
public:
//-------------------------------------------------------------------------
// Constructor
//-------------------------------------------------------------------------
UnboundedKnapsack()
{
maximumCapacity = -1;
}
//-------------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------------
~UnboundedKnapsack()
{
delete this;
}
//-------------------------------------------------------------------------
// Methods
//-------------------------------------------------------------------------
/**
* Unbounded knapsack allows to use one or more occurrences of an item.
*
* @param w Weight of the elements
* @param v Value of the elements
* @param N Number of itens
* @param W Maximum weight capacity
* @return This object to allow chained calls
*/
UnboundedKnapsack* knapsack_unbounded(std::vector<int>& w, std::vector<int>& v, int N, int W)
{
// Stores the maximum value which can be reached with a certain capacity
knapsack.clear();
knapsack.resize(W + 1);
maximumCapacity = W + 1;
// Stores selected elements with a certain capacity
selectedElements.resize(W + 1);
// Initializes maximum value vector with zero
for (int i = 0; i < W + 1; i++) {
knapsack[i] = 0;
}
// Computes the maximum value that can be reached for each capacity
for (int capacity = 0; capacity < W + 1; capacity++) {
// Goes through all the elements
for (int n = 0; n < N; n++) {
if (w[n] <= capacity) {
// max(knapsack[capacity], knapsack[capacity - w[n]] + v[n])
if (knapsack[capacity] <= knapsack[capacity - w[n]] + v[n]) {
knapsack[capacity] = knapsack[capacity - w[n]] + v[n];
// Stores selected elements
selectedElements[capacity].clear();
selectedElements[capacity].push_back(n + 1);
for (int elem : selectedElements[capacity - w[n]]) {
selectedElements[capacity].push_back(elem);
}
}
}
}
}
return this;
}
/**
* Returns maximum value for a certain number of elements and a certain
* capacity.
*
* @param capacity Capacity of the knapsack
* @return Maximum possible value with capacity provided
* @throws std::invalid_argument If capacity provided is out of bounds
*/
int getMaximumValue(int capacity)
{
if (capacity < 0 || capacity >= maximumCapacity)
throw std::invalid_argument("Capacity out of bounds");
return knapsack[capacity];
}
/**
* Returns elements that belong to the knapsack with a certain capacity.
*
* @param capacity Capacity of the knapsack
* @return Elements that are part of the knapsack with the capacity
* provided
* @throws std::invalid_argument If capacity provided is out of bounds
* @apiNote Elements are referenced by their index + 1
*/
std::vector<int>& getSelectedElements(int capacity)
{
if (capacity < 0 || capacity >= maximumCapacity)
throw std::invalid_argument("Capacity out of bounds");
return selectedElements[capacity];
}
/**
* Returns elements that are part of the knapsack with a certain capacity.
* This method will return a {@link std::string} with the following format:
* <code>[elem1, elem2, elem3...]</code>
*
* @param capacity Capacity of the knapsack
* @return Elements that are part of the knapsack with the capacity
* provided
* @apiNote Elements are referenced by their index + 1
*/
std::string selectedElements_toString(int capacity)
{
std::string response = "[";
for (int element : selectedElements[capacity]) {
response.append(std::to_string(element));
response.append(", ");
}
// Removes last ", "
response.pop_back();
response.pop_back();
response.append("]");
return response;
}
};
//-------------------------------------------------------------------------
// Main
//-------------------------------------------------------------------------
/**
* Example made based on this exercise:
* {@link https://www.urionlinejudge.com.br/repository/UOJ_1487_en.html}
*/
int main()
{
UnboundedKnapsack* knapsack = new UnboundedKnapsack();
int totalCapacity = 60, elements = 5;
std::vector<int> elements_weight = { 10, 20, 5, 50, 22 };
std::vector<int> elements_values = { 30, 32, 4, 90, 45 };
knapsack->knapsack_unbounded(elements_weight, elements_values, elements, totalCapacity);
std::cout << "Maximum value: "
<< knapsack->getMaximumValue(totalCapacity)
<< std::endl;
std::cout << "Selected elements: "
<< knapsack->selectedElements_toString(totalCapacity)
<< std::endl;
system("pause");
return 0;
}
输出
Maximum value: 180
Selected elements: [1, 1, 1, 1, 1, 1]
我希望这会有所帮助。如果您有兴趣,我还实现了一个版本,显示与经典版本的背包一起存储的元素。可在此处获得:
https://github.com/williamniemiec/algorithms/blob/master/Dynamic%20programming/knapsack/c%2B%2B/BoundedKnapsack.cpp