Login
Register
Problem list
Online status
wayne0220
:
2024-12-17 15:50:17
#include
#include
using namespace std; int main() { int Num; cin >> Num; for (int it = 0; it < Num; it++) { int n, capacity; cin >> n >> capacity; vector
weight(n); vector
value(n); for (int i = 0; i < n; i++) { cin >> weight[i] >> value[i]; } vector
dp(capacity + 1, 0); for (int i = 0; i < n; i++) { for (int j = capacity; j >= weight[i]; j--) { if(dp[j - weight[i]] != 0 || j == weight[i]) { // 1019的基础上加上这一句就OK了,装不满就不装 dp[j] = max(dp[j - weight[i]] + value[i], dp[j]); } } } cout << dp[capacity] << endl; } return 0; }
泘湖浒沪
:
2024-12-17 15:07:11
修改新的状态转移方程即可,装不满就是0
lin zehao
:
2023-05-04 16:59:56
此题和1019类似,1019是每个新宝石对所有容量背包判断一次拾取和不拾取这个宝石哪种情况价值更大(value[k] = max(value[k - s] + v, value[k]););1018只是添加一个条件,只有原来大小是满的才会判断是否拾取(if (bag_is_full[k - s] == 1)),若能拾取,则bag_is_full[k]=1。 时间复杂度依旧为o(n*c)
Post Your Comment