背包问题学习笔记(二)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                背包问题学习笔记(二)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                0-1背包問題解決代碼,騰訊的試題中,只需將物品的價值與物品的重量取一樣的值即可。
PackProblemClass.h:
// PackProblemClass.h: interface for the PackProblemClass class. // //#if !defined(AFX_PACKPROBLEMCLASS_H__60EB89B2_4A0F_4085_8973_AC42DE6842E5__INCLUDED_) #define AFX_PACKPROBLEMCLASS_H__60EB89B2_4A0F_4085_8973_AC42DE6842E5__INCLUDED_#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000class PackProblemClass { public:int S;//背包的容量為Sint N;//物品的件數(shù)為Nint *w;//物品的重量 N維數(shù)組int *v;//物品的價值 N維數(shù)組int *x;//背包裝的物品價值最大時對應(yīng)的物品種類 N維數(shù)組 x[i]=1表示第i個物品在背包中int V;//背包能裝下的最大價值void DynamicProgramming();//進(jìn)行動態(tài)規(guī)劃,計(jì)算最大價值V,和對應(yīng)的物品組合xvoid ResultOutput();//將動態(tài)規(guī)劃結(jié)果輸出PackProblemClass(int iS,int iN,int *iw,int *iv);//構(gòu)造函數(shù)virtual ~PackProblemClass();//析構(gòu)函數(shù)};#endif // !defined(AFX_PACKPROBLEMCLASS_H__60EB89B2_4A0F_4085_8973_AC42DE6842E5__INCLUDED_)?PackProblemClass.cpp:
// PackProblemClass.cpp: implementation of the PackProblemClass class. // //#include "stdafx.h" #include "PackProblemClass.h" #include <iostream>using namespace std;// // Construction/Destruction // //背包問題類 PackProblemClass::PackProblemClass(int iS,int iN,int *iw,int *iv) {this->S = iS;//背包的容量為Sthis->N = iN;//物品的件數(shù)為Nthis->w = iw;//物品的重量 N維數(shù)組this->v = iv;//物品的價值 N維數(shù)組this->V = 0;//背包能裝下的最大價值this->x = new int[N];//背包裝的物品價值最大時對應(yīng)的物品種類 N維數(shù)組 x[i]=1表示第i個物品在背包中for(int i=0;i<N;i++)//初始化x{x[i] = 0;} }void PackProblemClass::DynamicProgramming()//進(jìn)行動態(tài)規(guī)劃,計(jì)算最大價值V,和對應(yīng)的物品組合x {int temp_S = S + 1;int i,j; //動態(tài)定義并初始化二維數(shù)組int **c;c = new int*[N+1];for(i=0;i<=N;i++){c[i] = new int[temp_S];}for(i=0;i<=N;i++){for(j=0;j<=S;j++){c[i][j] = 0;}}////根據(jù)公式c[i][j]=MAX{c[i-1][j],c[i-1][j-w[i-1]+v[i-1]]}計(jì)算c[][]for(i=1;i<=N;i++){for(j=1;j<=S;j++){if (j>=w[i-1]) {if (c[i-1][j]<(c[i-1][j-w[i-1]]+v[i-1])) {c[i][j] = c[i-1][j-w[i-1]]+v[i-1];}else{c[i][j] = c[i-1][j];}}else{c[i][j] = c[i-1][j];}}}//輸出c[][]for(i=0;i<=N;i++){for(j=0;j<=S;j++){cout<<c[i][j]<<" ";}cout<<endl;}///逆推法計(jì)算最大值V時對應(yīng)的物品組合xV = c[N][S];temp_S = S;for(i=N;i>0;i--){if (c[i][temp_S]>c[i-1][temp_S]) {x[i-1] = 1;temp_S = temp_S - w[i-1];}}}void PackProblemClass::ResultOutput()//將動態(tài)規(guī)劃結(jié)果輸出 {cout<<"背包的容量:"<<S<<endl;cout<<"物品的件數(shù):"<<N<<endl;cout<<"物品的重量:"<<endl;int i;for(i=0;i<N;i++)cout<<w[i]<<" "<<endl;cout<<endl;cout<<"物品的價值:"<<endl;for(i=0;i<N;i++)cout<<v[i]<<" "<<endl;cout<<endl;cout<<"動態(tài)規(guī)劃的結(jié)果為:"<<endl;cout<<"背包所能裝下的物品的最大價值為:"<<V<<endl;cout<<"物品的種類:"<<endl;for(i=0;i<N;i++)cout<<x[i]<<" "<<w[i]<<" "<<v[i]<<endl;cout<<endl;}PackProblemClass::~PackProblemClass() {}?
轉(zhuǎn)載于:https://www.cnblogs.com/finalsatan/p/4057058.html
總結(jié)
以上是生活随笔為你收集整理的背包问题学习笔记(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 20 个强大的 Sublime Text
- 下一篇: reStructuredText学习
