【Rookie er】Dynamic programming_01 Backpack

#include <bits/stdc++.h>
#define MAX_N 1000
using namespace std;

int v[MAX_N],w[MAX_N];
int dp[100][100];
int n;
int wmax,vmax;

intmain()
{
    //enter
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>w[i]>>v[i];
    }
    cin>>wmax;
    /* Determine subproblems:
        The purpose is to get the maximum value, and the operation is to put the i-th item in the backpack?
    */
    /* Determine the meaning of dp[][]:
        dp[i][j]: i: The first i items participated in the operation;
                    j: knapsack with volume j;
                    dp[][]: maximum value in case of ij
                    ==: The maximum value of several items in the first i pieces into a knapsack of volume j.

    */
    /* Adjust the initial state: (boundary processing)
        x is any valid value
        dp[0][x] = 0; //The total value of the first 0 items in the backpack that supports x is always 0
        dp[x][0] = 0; //The size of the backpack is 0, and the total value is always 0;
    */
    /* The most exciting place: the determination of the state transition equation
        1. Same as adjusting the initial state
        2.
            If: the knapsack volume j is less than the sum of the first i objects
                The i-th object can no longer be packed on the back, and the total value is still the value of j-1.
            otherwise:{
                //To maximize the total value of objects in the backpack of volume V,
                // Should the ith item be put in the backpack?

                max (do not put the i-th item, put the i-th item but there is a cost of space reduction)
                //It is precisely because the space is reduced that the previous value can be found and compared

            }

            Code:
            if( j < w[i] ){
                f[i][j] = f[i-1][j];
            }
            else {
                f[i][j] = max( f[i-1][j] , f[i-1][j-w[i]]+v[i] );
            }

    */

    // just born
    /*//All are 0 when initialized, no need to operate again
    for(int i = 0;i<n;i++){
        dp[i][0]=0;
        dp[0][i]=0;
    }*/
    //People dp is super simple
    for(int i = 0;i<n;i++){
        for(int j = 0;j<=wmax;j++){
            if( j<w[i] ){
                dp[i+1][j] = dp[i][j];
            }
            else{
                dp[i+1][j] = max(dp[i][j],dp[i][j-w[i]]+v[i]);
            }

        }

    }
    //Output the memoized array, here in order to output a little more data
    int print_max = wmax>n ? wmax+1 : n+1;
    for(int i = 0;i<print_max;i++){
        for(int j = 0;j<print_max;j++)
            cout<<dp[i][j]<<" ";
        cout<<endl;
    }

    cout<<dp[n][wmax]<<endl;

    //Extension: If you want to choose which one to output, expand the max() function into an if();
    return 0;
}
/**
n items, the weight is represented by w and the value is represented by v,
Pick items whose total weight is not greater than wmax,
Find the maximum value vmax that can be picked out;
*/
/*
data:
4
2 3
1 2
3 4
2 2
5
print:
7
*/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325722283&siteId=291194637