题目
LP1757
思路
背包九讲
代码
复制代码
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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <utility>
#define _for(i,a,b) for(int i = (a); i<(b); i++)
#define _rep(i,a,b) for(int i = (a); i<=(b); i++)
using namespace std;
const int maxm = 1000 + 10;
const int maxt = 100 + 10;
int n, c, v, w, d[maxm];
vector<pair<int, int> > item[maxt];
int main() {
scanf("%d%d", &c, &n);
int s, t = 0;
_for(i, 0, n) {
scanf("%d%d%d", &v, &w, &s);
s--;
if (item[s].empty()) t++;
item[s].push_back(make_pair(v, w));
}
_for(i, 0, t) {
for (int j = c; j >= 0; j--)
for (vector<pair<int, int> >::iterator iter = item[i].begin(); iter != item[i].end(); ++iter)
if (j >= (*iter).first)
d[j] = max(d[j], d[j - (*iter).first] + (*iter).second);
}
int ans = 0;
_rep(i, 0, c) ans = max(ans, d[i]);
printf("%dn", ans);
return 0;
}
最后
以上就是文静小天鹅最近收集整理的关于[背包DP] 洛谷P1757 分组背包的全部内容,更多相关[背包DP]内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复