cv_gordon

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::

 

参考该文章

https://www.cnblogs.com/henuliulei/p/10041737.html

 

#include <iostream>
#include <cstring>

using namespace std;


int main (int argc, char** argv){
    
    int workerNumber = 10;
    int auNumber = 5;
    int maxVal = 0;
    int auVal[] = {100, 200, 150, 400, 300};
    int auCost[] = {3, 4, 3, 5, 5};
    bool choose[] = {false, false, false, false, false};
    int valueT[auNumber+1][workerNumber+1];
    std::memset(valueT, 0, sizeof(valueT));
    
    // initialize the valueT
    for(int i = 1; i <= auNumber; i++){
        for(int j = 1; j <= workerNumber; j++){
            if (j < auCost[i-1])
                valueT[i][j] = valueT[i-1][j];
            else
                valueT[i][j] = max(valueT[i-1][j], valueT[i-1][j-auCost[i-1]]+auVal[i-1]);
        }
    }
    
    for(int i = 0; i <= auNumber; i++){
        for(int j = 0; j <= workerNumber; j++){
            std::cout << valueT[i][j] << " ";
        }
        std::cout << std::endl;
    }
    
    // get the result
    int k = workerNumber;
    for(int i = auNumber; i > 0; i--){
        if(valueT[i][k] > valueT[i-1][k]){
            choose[i-1] = true;
            
            k = k - auCost[i-1];
        }        
    }
    
    for(int i = 0; i < auNumber; i++)
        cout << choose[i] << " ";
    
    return 0;
}

 

posted on 2019-08-11 15:15  cv_gordon  阅读(478)  评论(0编辑  收藏  举报