背包问题的动态规划C++实现(01背包和完全背包)

0,1背包

为方便起见,令零件价值等于其长度

#include
#include
#include
#include
using namespace std;
void zeroOneKnapsack(vector<vector<int>>& path,vector<int>& result,
	const vector<int>& v, const vector<int>& w, int C) {

	size_t n = v.size();
	//记录路径的数组
	vector<vector<int>> p(n, vector<int>(C + 1));
	vector<int> f(C+1);

	//初始化
	for (size_t j = 0; j < C + 1; j++)
	{
		if (j >= w[0])
		{
			p[0][j] = 1;
			f[j] = w[0] ;
		}
	}

	for (size_t i = 1; i < n; i++) {
		for (int j = C; j >= w[i]; j--) {
			if (f[j] < f[j - w[i]] + v[i])
			{
				f[j] = f[j - w[i]] + v[i];
				p[i][j] = 1;
			}
		}
	}
	result = f;
	path = p;
}

vector<int> zeroOneKnapsackBacktrackPlan(const vector<int>& w,const vector<vector<int>>& path, const vector<int>& result)
{

	size_t n = path.size();
	size_t C = result.size()-1;

	vector<int>plan(n);

	int i = n-1;
	int j = C;
	while (i >= 0) {
		if (path[i][j]) {
			plan .at(i) = 1;
			j -= w[i];
		}
		i--;
	}
	return plan;
}

template <typename T>
void printVector(const vector<T>& v)
{
	for (auto it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void printInfo(const vector<int>& weight, const vector<int>& value,const vector<int>& plan,const vector<int>& result)
{
	auto C = result.size()-1;
	auto n = weight.size();
	cout << "knapsack volume is:" << C << endl;
	cout << "weight:";
	printVector(weight);
	cout << "value:";
	printVector(value);
	cout << "solution:";
	printVector(plan);
	cout << "result:" << result[C] << endl;
	cout << endl;
}

int main(int argc, char* argv[])
{
	vector<vector<int>> weight
	{
		{20,80,30,70,23},
		{23,45,64,23,2},
		{1322,1900,345,235},
		{13,81,47,234,457,908,1423,453,2354,4200,4300},
		{1700,2300,354,677,4333,5647,435,809,234,457,908,1423,453,2354,4200,4300},
		{307,312,817,322,722,742,747,752,757,762,767,772,777,802,873,882,883,1073,1078,1083,1088,1093,1098,1103,1108,1113,1118,1123,1166,1326,562,572,577,582,587,592,597,602,607,612,617,622,722,742,747,752,757,762,767,772,777,802,873,882,552}
	};
	auto value = weight;
	vector<int>C = { 100,100,6000,6000,6000,6000};

	//0,1背包
	
    cout << "zeroOneKnapsack:" << endl;
	for (size_t i = 0; i < C.size(); i++)
	{
		clock_t startTime, endTime;
		vector<int> result;
		vector<vector<int>> path;
		vector<int> plan;
		

		startTime = clock();
		zeroOneKnapsack(path, result, value.at(i), weight.at(i), C.at(i));
		plan = zeroOneKnapsackBacktrackPlan(weight.at(i), path, result);
		endTime = clock();

		cout << setprecision(8);
		cout << "consume " << ((double)endTime - (double)startTime) / CLOCKS_PER_SEC << " seconds" << endl;
		printInfo(weight.at(i),value.at(i),plan,result);
		cout << "----------------------" << endl;
	}
}

函数zeroOneKnapsack求解背包问题

zeroOneKnapsackBacktrackPlan回溯获得方案

运行结果为

zeroOneKnapsack:
consume 0 seconds
knapsack volume is:100
weight:20 80 30 70 23
value:20 80 30 70 23
solution:1 1 0 0 0
result:100

----------------------
consume 0 seconds
knapsack volume is:100
weight:23 45 64 23 2
value:23 45 64 23 2
solution:1 1 0 1 1
result:93

----------------------
consume 0.001 seconds
knapsack volume is:6000
weight:1322 1900 345 235
value:1322 1900 345 235
solution:1 1 1 1
result:3802

----------------------
consume 0.001 seconds
knapsack volume is:6000
weight:13 81 47 234 457 908 1423 453 2354 4200 4300
value:13 81 47 234 457 908 1423 453 2354 4200 4300 
solution:1 1 1 1 0 0 1 0 0 1 0
result:5998

----------------------
consume 0.002 seconds
knapsack volume is:6000
weight:1700 2300 354 677 4333 5647 435 809 234 457 908 1423 453 2354 4200 4300
value:1700 2300 354 677 4333 5647 435 809 234 457 908 1423 453 2354 4200 4300 
solution:0 1 0 1 0 0 1 0 1 0 0 0 0 1 0 0
result:6000

----------------------
consume 0.004 seconds
knapsack volume is:6000
weight:307 312 817 322 722 742 747 752 757 762 767 772 777 802 873 882 883 1073 1078 1083 1088 1093 1098 1103 1108 1113 1118 1123 1166 1326 562 572 577 
582 587 592 597 602 607 612 617 622 722 742 747 752 757 762 767 772 777 802 873 882 552
value:307 312 817 322 722 742 747 752 757 762 767 772 777 802 873 882 883 1073 1078 1083 1088 1093 1098 1103 1108 1113 1118 1123 1166 1326 562 572 577 582 587 592 597 602 607 612 617 622 722 742 747 752 757 762 767 772 777 802 873 882 552
solution:1 1 1 1 0 0 0 0 1 0 1 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:6000

完全背包

完全背包只给出求解函数和回溯方案函数,其他信息和上面的一致

vector<int> completeKnapsackBacktrackPlan(const vector<int>& w, const vector<vector<int>>& path, int result)
{
	vector<int>plan(w.size());
	int i = path.size() - 1;
	int j = result;
	while (i >= 0) {
		if (path[i][j]) {
			plan.at(i) += 1;
			j -= w[i];
		}
		else
		{
			i--;
		}
	}
	return plan;
}

void completeKnapsack(vector<vector<int>>& path, vector<int>& result,
	const vector<int>& v, const vector<int>& w, int C)
{
	size_t n = v.size();
	//记录路径的数组
	vector<vector<int>> p(n, vector<int>(C + 1));
	vector<int> f(C + 1);
	//初始化
	for (int j = 0; j < C + 1; j++)
	{
		if (j /w[0]>=1)
		{
			p[0][j] = 1;
			f[j] = j/w[0]*w[0];
		}
	}

	for (int i = 1; i < n; i++) {
		for (int j = w[i]; j <= C; j++) {
			if (f[j] < f[j - w[i]] + v[i])
			{
				f[j] = f[j - w[i]] + v[i];
				p[i][j] = 1;
			}
		}
	}
	result = f;
	path = p;
}

运行结果如下所示

completeKnapsack:
consume 0 seconds
knapsack volumn is:100
weight:20 80 30 70 23
value:20 80 30 70 23
solution:5 0 0 0 0
result:100

----------------------
consume 0 seconds
knapsack volumn is:100
weight:23 45 64 23 2 
value:23 45 64 23 2
solution:4 0 0 0 4
result:100

----------------------
consume 0 seconds
knapsack volumn is:6000
weight:1322 1900 345 235
value:1322 1900 345 235
solution:2 0 7 4
result:5999

----------------------
consume 0.001 seconds
knapsack volumn is:6000
weight:13 81 47 234 457 908 1423 453 2354 4200 4300
value:13 81 47 234 457 908 1423 453 2354 4200 4300 
solution:393 11 0 0 0 0 0 0 0 0 0
result:6000

----------------------
consume 0 seconds
knapsack volumn is:6000
weight:1700 2300 354 677 4333 5647 435 809 234 457 908 1423 453 2354 4200 4300
value:1700 2300 354 677 4333 5647 435 809 234 457 908 1423 453 2354 4200 4300
solution:0 0 4 0 0 0 10 0 1 0 0 0 0 0 0 0
result:6000

----------------------
consume 0.005 seconds
knapsack volumn is:6000
weight:307 312 817 322 722 742 747 752 757 762 767 772 777 802 873 882 883 1073 1078 1083 1088 1093 1098 1103 1108 1113 1118 1123 1166 1326 562 572 577 
582 587 592 597 602 607 612 617 622 722 742 747 752 757 762 767 772 777 802 873 882 552
value:307 312 817 322 722 742 747 752 757 762 767 772 777 802 873 882 883 1073 1078 1083 1088 1093 1098 1103 1108 1113 1118 1123 1166 1326 562 572 577 582 587 592 597 602 607 612 617 622 722 742 747 752 757 762 767 772 777 802 873 882 552
solution:1 11 1 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:6000

----------------------

E:\cpp>cd "e:\cpp\" && g++ completeKnapsack.cpp -o completeKnapsack && "e:\cpp\"completeKnapsack
completeKnapsack:     
consume 0 seconds     
knapsack volumn is:100
weight:20 80 30 70 23 
value:20 80 30 70 23  
solution:5 0 0 0 0    
result:100

----------------------
consume 0 seconds     
knapsack volumn is:100
weight:23 45 64 23 2  
value:23 45 64 23 2
solution:4 0 0 0 4
result:100

----------------------
consume 0.001 seconds
knapsack volumn is:6000
weight:1322 1900 345 235
value:1322 1900 345 235
solution:2 0 7 4
result:5999

----------------------
consume 0.001 seconds
knapsack volumn is:6000
weight:13 81 47 234 457 908 1423 453 2354 4200 4300 
value:13 81 47 234 457 908 1423 453 2354 4200 4300 
solution:393 11 0 0 0 0 0 0 0 0 0
result:6000

----------------------
consume 0.003 seconds
knapsack volumn is:6000
weight:1700 2300 354 677 4333 5647 435 809 234 457 908 1423 453 2354 4200 4300
value:1700 2300 354 677 4333 5647 435 809 234 457 908 1423 453 2354 4200 4300
solution:0 0 4 0 0 0 10 0 1 0 0 0 0 0 0 0 
result:6000

----------------------
consume 0.004 seconds
knapsack volumn is:6000
weight:307 312 817 322 722 742 747 752 757 762 767 772 777 802 873 882 883 1073 1078 1083 1088 1093 1098 1103 1108 1113 1118 1123 1166 1326 562 572 577 
582 587 592 597 602 607 612 617 622 722 742 747 752 757 762 767 772 777 802 873 882 552
value:307 312 817 322 722 742 747 752 757 762 767 772 777 802 873 882 883 1073 1078 1083 1088 1093 1098 1103 1108 1113 1118 1123 1166 1326 562 572 577 582 587 592 597 602 607 612 617 622 722 742 747 752 757 762 767 772 777 802 873 882 552
solution:1 11 1 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
result:6000

参考文献

背包问题九讲

你可能感兴趣的:(算法,算法,动态规划)