揹包問題(Knapsack problem)是一種組合優化的NP徹底問題。
問題能夠描述爲:給定一組物品,每種物品都有本身的重量和價格,
在限定的總重量內,咱們如何選擇,才能使得物品的總價格最高。html
本文介紹使用動態規劃的方法解決此問題
算法思想:c++
代碼:算法
class PackageSolver { public: int sum_value_matrix[6][11]; int knapsack(PackageItem* items) { init(items); for (int c = 2; c <= 10; c++) { for (int n = 2; n <= 5; n++) { if (c < items[n].weight) { //揹包capacity 比第n項小 sum_value_matrix[n][c] = sum_value_matrix[n - 1][c]; } else { //裝得下第n項 //undo: c - items[n].weight>0? else? int temp1 = sum_value_matrix[n - 1][c]; int temp2 = items[n].value; if (c - items[n].weight > 0) { temp2 = sum_value_matrix[n - 1][c - items[n].weight] + items[n].value; } sum_value_matrix[n][c] = temp1 > temp2 ? temp1 : temp2; } } } return sum_value_matrix[5][10]; } //初始化 void init(PackageItem* items) { //初始化0 for (int n = 1; n <= 5; n++) { for (int c = 1; c <= 10; c++) { sum_value_matrix[n][c] = 0; } } //初始化第一列,第n件物品的weight value for (int n = 1; n <= 5; n++) { sum_value_matrix[n][1] = 0; } //初始化第一行,只裝第一個物品 容量爲c時候的 總value for (int c = 1; c <= 10; c++) { if (c >= items[1].value) { sum_value_matrix[1][c] = items[1].value; } //cout << sum_value_matrix[1][c]; } } void print() { cout<<"x:capacity y:top n items"<<endl; for (int n = 1; n <= 5; n++) { for (int c = 1; c <= 10; c++) { cout << sum_value_matrix[n][c] << ends; } cout << endl; } cout << endl; } };
struct PackageItem { string name; int weight; int value; PackageItem(string name, int weight, int value) { this->name = name; this->weight = weight; this->value = value; } void print() { cout << "name:" << name << ends << "weight:" << weight << ends << "value:" << value << endl; } };
class Problem2Tester { public: //爲了使得下標與實際意義一致,因此設置item[0]爲空 struct PackageItem items[6] = { { " ", 0, 0 }, { "a", 2, 2 }, { "b", 2, 3 }, { "c", 6, 5 }, { "d", 5, 4 }, { "e", 4, 6 }, }; PackageSolver ps; void item_test() { for (int i = 0; i <= 5; i++) { items[i].print(); } } void init_test() { ps.print(); ps.init(items); ps.print(); } void knapsack_test() { ps.init(items); ps.print(); ps.knapsack(items); ps.print(); } }tester2;
參考:
http://blog.csdn.net/mu399/article/details/7722810
http://blog.sina.com.cn/s/blog_6dcd26b301013810.html測試