Loading

AcWing.3. 完全背包问题

题目链接

https://www.acwing.com/problem/content/3/

题目思路

01背包进阶版,每种物品有无限个
所以就是将每种物品的体积和价值存下来,然后遍历每种物品,在当前体积的限制下求可拿物品的价值最优解
image

题目代码

#include <iostream>
#include <algorithm>

using namespace std;
const int N = 1010;
int f[N];
int v[N], w[N];
int n, t;

int main()
{
    cin >> n >> t;
    for(int i = 0; i < n; i ++ )
        cin >> v[i] >> w[i];
    
    for(int i = 0; i < n; i ++ )
        for(int j = v[i]; j <= t; j ++ )
            f[j] = max(f[j], f[j - v[i]] + w[i]);
    
    cout << f[t] << endl;
    return 0;
}
posted @ 2022-03-31 19:57  vacilie  阅读(37)  评论(0)    收藏  举报