-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsack.hpp
More file actions
29 lines (23 loc) · 811 Bytes
/
Copy pathknapsack.hpp
File metadata and controls
29 lines (23 loc) · 811 Bytes
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
# ifndef KNAPSACK_HPP
# define KNAPSACK_HPP
# include <vector>
# include <set>
std::set<int> knapsack(int subjects, int wl, std::vector<int> &workloads, std::vector<int> &prefs) {
std::set<int> ans;
std::vector<std::vector<int>> dp(subjects + 1, std::vector<int>(wl + 1));
for (int i = 1; i <= subjects; ++i) {
for (int j = 1; j <= wl; ++j) {
if (workloads[i - 1] > j) dp[i][j] = dp[i - 1][j];
else dp[i][j] = std::max(dp[i - 1][j - workloads[i - 1]] + prefs[i - 1], dp[i - 1][j]);
}
}
int i = subjects, j = wl;
while (j > 0 && i > 0) {
if (dp[i][j] != dp[i - 1][j]) {
ans.insert(i - 1);
j -= workloads[i - 1]; --i;
} else --i;
}
return ans;
}
# endif /* end of include guard: KNAPSACK_HPP */