是在 hydro 上做的,墙裂推荐 hydro 的一本通题库!链接是:https://hydro.ac/d/ybttk/p/T1270。
分析一下,可以分类讨论,处理的时候,零一背包(\(p_i = 1\))一个,完全背包(\(p_i = 0\))一个,多重背包(\(p_i > 1\)) 一个,状态转移方程不用列的,直接去抄模板就可以啦~
代码是这样的捏:
#include <bits/stdc++.h>
using namespace std;
const int M = 205;
const int N = 35;
int dp[M], w[N], c[N], p[N];
int main(){
int n, m;
cin >> m >> n;
for (int i = 0; i < n; i ++){
cin >> w[i] >> c[i] >> p[i];
}
for (int i = 0; i < n; i ++){
if (p[i] == 1){
for (int j = m; j >= w[i]; j --){
dp[j] = max(dp[j], dp[j - w[i]] + c[i]);
}
}else if (p[i] == 0){
for (int j = w[i]; j <= m; j ++){
dp[j] = max(dp[j], dp[j - w[i]] + c[i]);
}
}else{
for (int j = m; j >= w[i]; j --){
for (int k = 0; k * w[i] <= j && k <= p[i]; k ++){
dp[j] = max(dp[j], dp[j - k * w[i]] + k * c[i]);
}
}
}
}
cout << dp[m];
}
撒花 /gz
标签:背包,1270,题解,cin,int,dp,hydro From: https://www.cnblogs.com/mayile/p/18062656/ybt1207-sol