Files
python/C++专题课程/动态规划/动态规划-2-完全背包.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

27 lines
574 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<bits/stdc++.h>
using namespace std;
/*
知识点内容:动态规则完全背包
文档内容参考:
https://www.cnblogs.com/aiguona/p/7274876.html
*/
using namespace std;
int w[300], c[300], f[300010];
int V, n;
int main() {
scanf("%d%d", &V, &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &w[i], &c[i]);
}
for (int i = 1; i <= n; i++)
for (int j = w[i]; j <= V; j++)//注意此处,与0-1背包不同,这里为顺序,0-1背包为逆序
f[j] = max(f[j], f[j - w[i]] + c[i]);
printf("max=%d\n", f[V]);
return 0;
}