0-1背包问题.(c++版)

 #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;
 
}

转载于:https://my.oschina.net/SHIHUAMarryMe/blog/547476

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值