装载问题子集树回溯

装载问题

问题描述:有一批共n个集装箱要装上2艘载重量分别为c1和c2的轮船,其中集装箱i的重量为wi,且在这里插入图片描述
,装载问题要求确定是否有一个合理的装载方案可将这些集装箱装上这2艘轮船。如果有,找出一种装载方案。

 例如:当n=3,c1=c2=50,且w=[10,40,40]时,则可以将集装箱1和2装到第一艘轮船上,而将集装箱3装到第二艘轮船上;如果w=[20,40,40],则无法将这3个集装箱都装上轮船。

基本思路: 容易证明,如果一个给定装载问题有解,则采用下面的策略可得到最优装载方案。
(1)首先将第一艘轮船尽可能装满;
(2)将剩余的集装箱装上第二艘轮船。
将第一艘轮船尽可能装满等价于选取全体集装箱的一个子集,使该子集中集装箱重量之和最接近C1。由此可知,装载问题等价于以下特殊的0-1背包问题。

template<class type>
 class Loading {
 public:
     Loading(type*,int, type);
     template<class T>
     friend void MaxLoading(Loading<T> load);
     void outPut();
     void Backtrack(int i);
 private:
     int n; //集装箱数
     type* w,//集装箱重量数组
         c,//第一艘载重
         cw,//当前的载重
         bestw;//当前最优的载重
 };

 template<class type>
 Loading<type>::Loading(type* ty,int n,type c)
 {
     w = ty;
     this->n = n;
     this->c = c;
     cw = 0;
     bestw = 0;
}

 template<class type>
 void Loading<type>::outPut()
 {
     std::cout << "集装箱数" << n << "第一艘载重:" << c << "当前的载重:" << cw << "当前最优的载重:" << bestw << std::endl;
     for (int i = 0; i < 3; i++)
     {
         std::cout << w[i] << std::endl;
     }
 }


 template<class type>
 void MaxLoading(Loading<type> load)
 {
     std::cout << "最大重量:" << load.bestw << std::endl;
 }


 //type* w,//集装箱重量数组
 //    c,//第一艘载重
 //    cw,//当前的载重
 template<class type>
 void Loading<type>::Backtrack(int t)
 {
     if (t > n)
     {
         if (bestw < cw)
             bestw = cw;
     }
     else
     {
         if (cw+w[t] < c)
         {
             cw += w[t];
             Backtrack(t + 1);
             cw -= w[t];
         }
         Backtrack(t + 1);
     }
 }



int main(){


    int num[3] = { 20,40,40 };
    Loading<int> load(num, 3, 50);
    load.outPut();
    load.Backtrack(1);
    MaxLoading(load);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值