跳到正文

题解

题解 P1616 【疯狂的采药】

24680esz约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
题目
P1616

版本

  1. v1当前

裸的完全背包,把01背包的代码略加修改就可以了

C++
#include <iostream>
using namespace std;
int t,m,w[100001]={0},v[100001]={0},f[100001]={0};//优化为1维
int main()
{
    cin>>t>>m;
    for (int i=1;i<=m;i++) cin>>w[i]>>v[i];
    for (int i=1;i<=m;i++)
      for (int j=w[i];j<=t;j++)//将01背包的倒序改为顺序
        if (f[j]<=f[j-w[i]]+v[i]) f[j]=f[j-w[i]]+v[i];//核心方程式
    cout<<f[t]<<endl;
    return 0;
}
原文