您的位置:首页 > 编程语言 > C语言/C++

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: