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

32 lines
664 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;
/*
知识点内容:动态规则0-1背包问题
文档内容参考:
https://www.cnblogs.com/aiguona/p/7274222.html
*/
using namespace std;
int w[105], val[105];
int dp[105][1005];
int main() {
int t, m;
cin >> t >> m;
for (int i = 1; i <= m; i++)
cin >> w[i] >> val[i];
for (int i = 1; i <= m; i++) //物品
for (int j = t; j >= 0; j--) //容量
{
if (j >= w[i])
dp[i][j] = max(dp[i - 1][j - w[i]] + val[i], dp[i - 1][j]);
else
dp[i][j] = dp[i - 1][j];
}
cout << dp[m][t] << endl;
return 0;
}