-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0005.h
More file actions
74 lines (66 loc) · 1.54 KB
/
Problem0005.h
File metadata and controls
74 lines (66 loc) · 1.54 KB
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
//
// Created by Fengwei Zhang on 2021/6/17.
//
#ifndef ACWINGSOLUTION_PROBLEM0005_H
#define ACWINGSOLUTION_PROBLEM0005_H
#include <iostream>
#include <cstring>
using namespace std;
class Problem0005
{
private:
struct Item
{
int v;
int w;
int s;
Item()
{
v = 0;
w = 0;
s = 0;
}
};
int knapsack_max_value(const int &n, const int &m, const Item *items)
{
int dp[m + 1];
memset(dp, 0, sizeof dp);
for (int i = 0, v, w, s; i < n; ++i)
{
s = items[i].s;
for (int factor = 1; factor <= s; factor *= 2)
{
v = items[i].v * factor;
w = items[i].w * factor;
for (auto j = m; j >= v; --j)
{
dp[j] = max(dp[j], dp[j - v] + w);
}
s -= factor;
}
if (s > 0)
{
v = items[i].v * s;
w = items[i].w * s;
for (auto j = m; j >= v; --j)
{
dp[j] = max(dp[j], dp[j - v] + w);
}
}
}
return dp[m];
}
int main()
{
int n, v;
scanf("%d%d", &n, &v);
Item items[n];
for (int i = 0; i < n; ++i)
{
scanf("%d%d%d", &items[i].v, &items[i].w, &items[i].s);
}
printf("%d\n", knapsack_max_value(n, v, items));
return 0;
}
};
#endif // ACWINGSOLUTION_PROBLEM0005_H