题目:
代码(首刷看解析 2024年2月22日):
#include<vector>
#include<iostream>
using namespace std;
int Pacakge(vector<int>& a,vector<int>& weights, vector<int>& values) {int M = a[0];//物品种类int N = a[1];//背包大小vector<vector<int>> dp(M, vector<int>(N + 1, 0));for (int i = weights[0]; i <= N ; ++i) {dp[0][i] = values[0];}for (int i = 1; i < M; ++i) {for (int j = 1; j < N + 1; ++j) {if (j < weights[i]) dp[i][j] = dp[i - 1][j];else dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - weights[i]] + values[i]);cout<<dp[i][j]<<" ";}cout<<endl;}return dp[M - 1][N];
}int main() {vector<int> a = {6, 1};vector<int> weights = {2, 2, 3, 1, 5, 2};vector<int> values = {2, 3, 1, 5, 4, 3};int res = Pacakge(a,weights,values);cout<<"res = "<<res<<endl;return 0;
}