0-1背包问题.(c++版)
2015-12-18 00:00
399 查看
#include <iostream> #include <type_traits> #include <memory>
template<typename T=int> class KnapsackProblem{ private: T* GateValue; T* GateWeight; //T* tempValue; //T* tempWeight; T* BestSubset; void sortWeight();//对物品的价值进行排序从小到大. int length; public: template<typename U=typename std::enable_if<std::is_integral<T>::value, void>::type>//判断类型T和U是否有一致性. KnapsackProblem(const std::initializer_list<U>& ValueList, const std::initializer_list<U>& WeightList); ~KnapsackProblem(); void print(const int& c=100); void GreedyArthemtic(const int& capacity=100);//这里的参数是背包的容量. };
template<typename T> template<typename U> KnapsackProblem<T>::KnapsackProblem(const std::initializer_list<U>& ValueList, const std::initializer_list<U>& WeightList) :GateValue(nullptr), GateWeight(nullptr), BestSubset(nullptr), length(ValueList.size()) { if(ValueList.size() != WeightList.size()){ throw std::bad_cast(); } int temp=ValueList.size();//获得珠宝的数量. GateValue=new U[temp+1]; GateWeight=new U[temp+1]; BestSubset=new U[temp+1]; //tempValue=new U[temp+1]; //tempWeight=new U[temp+1]; //this->tempValue[0]=0; //this->tempWeight[0]=0; this->GateValue[0]=0; this->GateWeight[0]=0; this->BestSubset[0]=0; std::uninitialized_copy_n(ValueList.begin(), temp, (this->GateValue+1)); std::uninitialized_copy_n(WeightList.begin(), temp, (this->GateWeight+1)); std::uninitialized_fill_n((this->BestSubset+1), temp, 0); //std::uninitialized_fill_n((this->tempValue+1), temp, 0); //std::uninitialized_fill_n((this->tempValue+1), temp, 0); std::cout<<"success"<<std::endl; }
template<typename T> KnapsackProblem<T>::~KnapsackProblem() { if(this->GateValue != nullptr){ delete[] GateValue; } if(this->GateValue != nullptr){ delete[] GateWeight; } std::cout<<"destroy"<<std::endl; }
template<typename T> void KnapsackProblem<T>::sortWeight()//插入排序. { for(int j=2; j<=this->length; ++j){//这里根据珠宝的质量进行降序排序. int value=GateValue[j]; int weight=GateWeight[j]; int i=j-1; while(i>0 && GateWeight[i]>weight){ GateValue[i+1]=GateValue[i]; GateWeight[i+1]=GateWeight[i]; --i; } this->GateValue[i+1]=value; this->GateWeight[i+1]=weight; } }
template<typename T> void KnapsackProblem<T>::print(const int& c) { this->sortWeight(); this->GreedyArthemtic(c); for(int i=1; i<=this->length; ++i){ std::cout<<this->BestSubset[i]<<" "; } std::cout<<std::endl; }
/* packageCapacity:背包的容量.(如果是先调用了sortWeight()那么此时是排序过的. this->lenght:珠宝的数量. */ template<typename T> void KnapsackProblem<T>::GreedyArthemtic(const int& capacity)//0-1背包问题. { if(GateWeight == nullptr || GateValue == nullptr){ throw std::bad_cast(); } int i=1; int packageCapacity=capacity; T weight=GateWeight[1]; for(; i<=this->length; ++i){ //length表示珠宝的数量。 if(this->GateWeight[i] > packageCapacity){ //如果一个珠宝的重量就超过背包能承受的最大重量那么只能舍弃咯. continue; } this->BestSubset[i]=i; //把放入背包的珠宝做备份. packageCapacity=packageCapacity-GateWeight[i]; //获得背包的剩余容量. //std::cout<<packageCapacity<<" "; } }
int main() { KnapsackProblem<> myKnapsack({20, 50, 30}, {100, 60, 120}); myKnapsack.print(); return 0; }
相关文章推荐
- C 语言的数据,常量和变量
- 帮 C/C++ 程序员彻底了解链接器
- c++将基本类型转为字符串写入文件
- 实战c++中的vector系列--vector<unique_ptr<>>赋值给vector<unique_ptr<>>
- vs2013报错 无法打开包含文件 "winsock2.h" 的一种解决办法
- 实战c++中的vector系列--vector<unique_ptr<>>赋值给vector<unique_ptr<>>
- 程序设计C语言-静态链表及指针
- C语言 malloc
- 用C语言实现简单的三子棋小游戏
- C++11: std::forward
- C++开发者都应该使用的10个C++11特性
- VC++代码刷新桌面图标和任务栏图标
- C++11笔记
- C语言学习笔记(21) 函数指针、回调函数及复杂指针阅读技巧
- 关于C中scanf函数的%c解释符的探究
- C++编程实践: 继承与多态
- typedef用法
- C++ Stream
- C++中new申请的内存, 可以用free释放吗?
- 你应当如何学习C++(以及编程)