动态规划解决背包问题

目录

动态规划步骤:

1.01背包问题

2.完全背包问题


动态规划步骤:

 step1.分析问题,定义dp数组(下标含义)

step2.初始化dp数组(边界)

step3.写dp状态转换方程(明确dp数组遍历顺序)

1.01背包问题

        即物品均只能取1次

46. 携带研究材料(第六期模拟笔试) (kamacoder.com)icon-default.png?t=N7T8https://kamacoder.com/problempage.php?pid=1046

#include <iostream>
#include <vector>
using namespace std;

void debug_input(vector<int>& vec){
    for(auto it=vec.begin(); it!=vec.end(); it++){
        cout<<*it<<" ";
    }
    cout<<endl;
}

class solution
{
public:
    int max_value(vector<int>& space, vector<int>& value, int M, int N){
        // 定义dp[i][j]
        vector<vector<int>> dp(M+1, vector<int> (N+1, 0));
        //初始化
        for(int j=space[0]; j<=N; j++)
            dp[0][j] = value[0];
        //状态转换
        for(int i=1; i<M; i++){
            for(int j=1; j<=N; j++){
                if(j < space[i]) dp[i][j] = dp[i-1][j];
                else dp[i][j] = max( dp[i-1][j], dp[i-1][j-space[i]] + value[i]); 
            }
        }
        return dp[M-1][N];
    }
};

int main(){
    // input
    int M,N,input;
    cin>>M>>N;
    vector<int> space(M,0);
    vector<int> value(M,0);
    for(int i=0; i<M; i++){
        cin>>input;
        space[i] = input;
    }
    for(int i=0; i<M; i++){
        cin>>input;
        value[i] = input;
    }
    // // debug input
    // debug_input(space);
    // debug_input(value);
    
    //solve
    solution mysolve;
    cout<<mysolve.max_value(space, value, M, N);
    
    return 0;
}

2.完全背包问题

        即满足条件下,每个物品可取无数次。 

52. 携带研究材料(第七期模拟笔试) (kamacoder.com)icon-default.png?t=N7T8https://kamacoder.com/problempage.php?pid=1052 

#include <iostream>
#include <vector>
using namespace std;

void debug_input(vector<int>& vec){
    for(auto it=vec.begin(); it!=vec.end(); it++){
        cout<<*it<<" ";
    }
    cout<<endl;
}

class solution
{
public:
    int max_value(vector<int>& space, vector<int>& value, int total_num, int total_space){
        // 定义dp[i][j]
        vector<vector<int>> dp(total_num, vector<int> (total_space+1, 0));
        //初始化
        for(int j=space[0]; j<=total_space; j++){
            dp[0][j] = dp[0][j-space[0]] + value[0];
        }
        //状态转换
        for(int i=1; i<total_num; i++){
            for(int j=1; j<=total_space; j++){
                if(j < space[i]) dp[i][j] = dp[i-1][j];
                else dp[i][j] = max( dp[i-1][j], dp[i][j-space[i]]+value[i]);
            }
        }
        return dp[total_num-1][total_space];
    }
};

int main(){
    //input
    int total_num, total_space;
    cin>>total_num>>total_space;
    vector<int> space(total_num,0);
    vector<int> value(total_num,0);
    int s, v;
    for(int i=0; i<total_num; i++){
        cin>>s>>v;
        space[i] = s;
        value[i] = v;
    }
    // debug_input(space);
    // debug_input(value);
    
    //solve
    solution mysolve;
    cout<<mysolve.max_value(space, value, total_num, total_space);
    return 0;
}

相关推荐

  1. 动态规划算法解决背包问题(Python)

    2024-04-13 17:58:04       56 阅读
  2. 动态规划学习——背包问题

    2024-04-13 17:58:04       52 阅读
  3. 动态规划】【背包问题】基础背包

    2024-04-13 17:58:04       43 阅读
  4. 动态规划-背包问题-二维费用背包 & 分组背包

    2024-04-13 17:58:04       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 17:58:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 17:58:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 17:58:04       82 阅读
  4. Python语言-面向对象

    2024-04-13 17:58:04       91 阅读

热门阅读

  1. 蓝桥杯省赛最后一天冲刺!!

    2024-04-13 17:58:04       36 阅读
  2. 在页面上清除多行数据

    2024-04-13 17:58:04       33 阅读
  3. isinstance函数详解

    2024-04-13 17:58:04       33 阅读
  4. LeetCode 17.电话号码的字母组合

    2024-04-13 17:58:04       35 阅读
  5. ActiveMQ消息中间件面试专题

    2024-04-13 17:58:04       33 阅读
  6. Android 自定义解析html标签

    2024-04-13 17:58:04       39 阅读
  7. golang 协程题目

    2024-04-13 17:58:04       39 阅读
  8. 自用-常用词

    2024-04-13 17:58:04       25 阅读