又见01背包

在这里插入图片描述
题目出处

题目描述: 01背包板子

思路:

如果dp 表示 体积为j的最大价值,发现体积最大 1 e 9 1e9 1e9,存不下,那么我们可以转换思路dp[j]:表示价值为j下最小的体积,然后倒找第一个不超过W的价值;

code

/*
    author:@bzdhxs
    date:2022/1/1
    URL:https://nyoj.online/problem/860

*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
template <typename T>
inline void read(T &s){s = 0;T w = 1, ch = getchar();while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }while (isdigit(ch))  { s = (s << 1) + (s << 3) + (ch ^48); ch = getchar();} s *= w;}
template <typename T>
inline void write(T s){if (s < 0) putchar('-'), s = -s;if (s > 9) write(s / 10);putchar(s % 10 + '0');}
#define _orz ios::sync_with_stdio(false),cin.tie(0)
#define mem(str,num) memset(str,num,sizeof(str))
#define forr(i,a,b) for(int i = a; i <= b;i++)
#define forn(i,n) for(int i = 0; i < n; i++)
#define dbg() cout <<"0k!"<< endl;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e4+10;

int n,m;
int v[110],w[110];
int f[N+1];
int main()
{
    while(cin >> n >> m){
        int sum = 0;
        forr(i,1,n) cin >> v[i] >> w[i],sum += w[i];
        mem(f,inf);//初始化为无穷大   const int inf = 0x3f3f3f3f;
        f[0] = 0;
        forr(i,1,n)for(int j = N; j >= w[i]; j--){
            f[j] = min(f[j],f[j-w[i]]+v[i]);
        }
        for(int i = N; i>=0;i--){
            if(f[i] <= m) {
                cout << i << endl;
                break;
            }
        }
     }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值