P1757 通天之分组背包

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
#include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXn = 1000;
const int MAXW = 1000;

int n, W;
int v[MAXn + 10];
int w[MAXn + 10];
int team[MAXn + 10][MAXn + 10];
int d[MAXW + 10];

inline int read() {
register char c;
for (c = getchar(); (c < '0' || c >'9') && c != '-'; c = getchar());
register bool f = c == '-';
register int s = f ? 0 : c - '0';
for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) {
s = (s << 3) + (s << 1) + c - '0';
}
return f ? -s : s;
}

void put_in() {
W = read();
n = read();
int teamid;
for (int i = 1; i <= n; i++) {
w[i] = read();
v[i] = read();
teamid = read();
team[teamid][++team[teamid][0]] = i;
}
}

void rec(int v[], int w[], int W)
{
for (int i = 1; i <= MAXn; i++) {
if (team[i][0]) {
for (int j = W; j >= 0; j--) {
for (int k = 1; k <= team[i][0]; k++) {
if (j >= w[team[i][k]]) d[j] = max(d[j], d[j - w[team[i][k]]] + v[team[i][k]]);
}
}
}
}
}

int eva_maxV(int d[], int W) {
int ans = -INF;
for (int i = 0; i <= W; i++) ans = max(ans, d[i]);
return ans;
}

int main() {
put_in();
rec(v, w, W);
cout << eva_maxV(d, W);
}