-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathknapsack.hpp
86 lines (70 loc) · 1.96 KB
/
knapsack.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream> // cin, cout
#include <vector> // vector
// @Problem
// Given a set of items, each with a weight and a value,
// determine the number of each item to include in a collection
// so that the total weight is less than or equal to a given limit
// and the total value is as large as possible.
//
// @Example
//
// ╔════════╦════╦═════╦═════╗
// ║ value ║ 60 ║ 100 ║ 120 ║
// ╠════════╬════╬═════╬═════╣
// ║ weight ║ 10 ║ 20 ║ 30 ║
// ╚════════╩════╩═════╩═════╝
// MAXW = 50
//
// Weight = 10; Value = 60;
// Weight = 20; Value = 100;
// Weight = 30; Value = 120;
// Weight = 10 + 20; Value = 160;
// Weight = 10 + 30; Value = 180;
// Weight = 20 + 30; Value = 220;
// Weight = 30 + 20 + 10 > 50
//
// Answer: 220;
//
// @Input format
// 50
// 3
// 60 10 100 20 120 30
//
// @Output format
// 220
#ifndef KNAPSACK_HPP
#define KNAPSACK_HPP
/**
* 0-1 knapsack solution class
*/
class Knapsack
{
using size_t = std::size_t;
size_t capacity;
std::vector<size_t> values ;
std::vector<size_t> weights;
bool calculated { false };
size_t result;
public:
Knapsack() = default;
Knapsack( size_t capacity, const std::vector<size_t>& values, const std::vector<size_t>& weights)
: capacity{ capacity }, values{ values }, weights{ weights } { }
size_t get_capacity() { return capacity; }
std::vector<size_t> get_values () { return values ; }
std::vector<size_t> get_weights() { return weights; }
/**
* Solved only first time, for others just use saved value
*/
size_t operator()()
{
return calculated ? result : calculated = true, solve();
}
friend std::ostream& operator<<( std::ostream& os, Knapsack& knp );
friend std::istream& operator>>( std::istream& is, Knapsack& knp );
private:
/**
* Algorithm implementation
*/
size_t solve();
};
#endif // KNAPSACK_HPP