编辑代码

#include <iostream>
using namespace std;
struct Item {
    string name;
    int weight;
    int value;
    Item(string n, int w, int v) {
        name = n;
        weight = w;
        value = v;
    }
    Item() {
        name = "";
        weight = 0;
        value = 0;
    }
};
int backpack(int packCapacity, Item *items, int itemCount) { 
    int minWeight = items[0].weight;
    for (int i = 1; i < itemCount; ++i)
    {
        int curWeight = items[i].weight;
        if (curWeight < minWeight) {
            minWeight = curWeight;
        }
    }
    if (packCapacity < minWeight) {
        cout << "The capacity of package " 
             << packCapacity << " is less than the minimum weight of items " 
             << minWeight << endl;
        return -1;
    }
    //创建表格,横轴是物品,纵轴是包能放入的物品的重量
    int weightCount = packCapacity + 1;
    int** dpArray = new int*[itemCount]();
    for (int i = 0; i < itemCount; ++i) {
        dpArray[i] = new int[weightCount];
    }
    // 填充表格
    for (int i = 0; i < itemCount; ++i)
    {
        // 记录放入背包物品的重量和价值
        int curWeight = items[i].weight;
        int curValue = items[i].value;
        for (int w = minWeight; w < weightCount; ++w)
        {
            // 记录不放入当前物品的情况下,放入背包物品能够达到的最大价值
            int preTotalValue= 0;
            if (i > 0) {
                preTotalValue = dpArray[i - 1][w];
            }
            // 记录放入当前物品的情况下,放入背包物品能够达到的最大价值
            int curTotalValue = 0;
            // 如果当前物品能够放入背包,记录下物品的价值
            if (w >= curWeight) {
                curTotalValue = curValue;
            }
            // 如果放入当前物品后背包还能放入其它物品,且确实还有其它物品,加上剩余的小背包能够放入物品的最大价值
            if ( w > curWeight && i > 0 ) {
                curTotalValue += dpArray[i-1][w - curWeight];
            }
            // 找出放入当前物品和不放入当前物品情况下,放入背包的物品能够达到的最大价值
            int maxTotalValue = preTotalValue;
            if (maxTotalValue < curTotalValue) {
                maxTotalValue = curTotalValue;
            }
            // 记录下放入当前物品后,能够放w磅物品的背包能够放入物品的最大价值
            dpArray[i][w] = maxTotalValue;

        }    
    }
    //记录下最终的最大价值
    int maxValue = dpArray[itemCount - 1][weightCount - 1];
    for (int i = 0; i < itemCount; ++i) {
        delete [] dpArray[i];
    }
    delete [] dpArray;  
    return maxValue;
}
int main() 
{
    int packCapacity = 10;
    Item items[] = {     
        Item("A", 1,2),
        Item("B", 2,3),
        Item("C", 4,5),
        Item("D", 5,3),
        Item("E", 6,5),
    };
    int itemCount = sizeof(items)/sizeof(Item);
    int maxValue = 0;
    maxValue = backpack(packCapacity, items, itemCount);
    if (maxValue > 0) {
        cout << "Max value is " << maxValue << endl;
    }    
}