• 6-2 装载问题(分支限界)


    6-3 装载问题(分支限界)

    一、问题描述

    有一批共个集装箱要装上2艘载重量分别为C1和C2的轮船,其中集装箱i的重量为Wi,且
    在这里插入图片描述
    采用下面的策略可得到最优装载方案:
    (1)将第一艘轮船尽可能装满;
    (2)将剩余集装箱装上第二艘轮船;

    二、问题分析

    1、队列式分支限界法

    在这里插入图片描述
    在这里插入图片描述
    ⒉.算法的改进(右子树加入剪枝条件)
    上述算法中右子没有剪枝,效率较差。
    策略:设bestw是当前最优解;ew是当前扩展结点所相应的重量;r是剩余集装箱的重量。
    则当ew+rsbestw时,可将其右子树剪去。
    为确保右子树成功剪枝,算法每一次进入左子树的时候更新bestw的值。不要等待i=n时才去更新。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    三、代码

    //队列式分支限界法  
    /*没有构造最优解
    10 3
    3 4 5
    
    10 4
    3  4 5 6
    */
    #include 
    #include 
    using namespace std;
    queue<int>q;
    int w[100]; //集装箱的重量 
    int c;
    int r;//r剩余没装的总重量 ,r的初值为全部集装箱重量之和 
    int n;//n个集装箱 
    int bestw=0;//当前最优载重量 
    void MaxLoading(){
    	int top=0;
    	int left=r;
    	q.push(-1);
    	int i=1;//第i层 
    	r=r-w[1];
    	while(i<=n&& !q.empty()){
    		if(i==1){
    			if(top+w[i]<=c){
    				q.push(top+w[i]);	
    				cout<<top+w[i]<<" ";
    			}
    			q.push(top); 
    			cout<<top<<" ";
    		}
    		top=q.front();
    		q.pop();
    		if(top==-1&&!q.empty()){
    			q.push(-1);
    			cout<<"-1\n";
    			i++;
    			top=q.front();
    			q.pop();
    			r=r-w[i];
    		}
    		int wt=top+w[i];
    		if(wt<=c){
    			if(wt>bestw) bestw=wt;
    			if(i<n){
    				q.push(wt);
    				cout<<wt<<" ";
    			} 
    		}
    		if(top+r>bestw){
    			if(i<n){
    				q.push(top);
    				cout<<top<<" ";
    			} 
    		}
    	}
    		
    } 
    int main(){
    	cin>>c;
    	cin>>n;
    	for(int i=1;i<=n;i++){
    		cin>>w[i];	
    		r=r+w[i];
    	}	
    	cout<<"----------\n";
    	MaxLoading();
    	cout<<"bestw="<<bestw<<endl; 
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    四、优先队列分支限界法

    在这里插入图片描述
    在这里插入图片描述

    //6-3装载问题 优先队列分支限界
    //优先队列分支限界法  
    /*
    10 3
    3 4 5
    */
    #include 
    #include 
    #include
    using namespace std;
    typedef struct QNode{
    	int w;
    	int priority;
    	int nexti;
    	int x[100];
    }node;
    bool operator < (node a, node b){
    	return a.priority < b.priority;
    }
    priority_queue<node, vector<node>, less<node> > q;
    int w[100]; //集装箱的重量 
    int c;
    int r;//r剩余没装的总重量 ,r的初值为全部集装箱重量之和 
    int n;//n个集装箱 
    int bestw=0;//当前最优载重量 
    int leftr[100];
    void Print(node no,int n){
    	cout<<"no.w="<<no.w<<endl;
    	for(int i=1;i<=n;i++){
    		cout<<no.x[i]<<" ";
    	}
    	cout<<endl;
    }
    void CalculateR(){
    	for(int i=1;i<=n;i++){
    		leftr[i]=r-w[i];
    		r-=w[i];
    	}
    }
    void MaxLoading(){
    	node no; 
    	node top;
    	top.w=0;
    	int i=1;//第i层 
    	while(i<=n){
    		r=leftr[i];
    		int wt=top.w+w[i];
    		if(wt<=c){
    			if(wt>bestw){
    				bestw=wt;
    			} 
    			no.w=wt;
    			no.priority = wt+r;
    			no.nexti=i+1;
    			memcpy(no.x,top.x,sizeof(top.x));//把top.x数组复制到no.x数组 
    			no.x[i]=1;
    			cout<<no.w<<" "<<no.priority<<" "<<no.nexti<<endl; 
    			Print(no,i);
    			q.push(no);	
    		}
    		if(top.w+r>=bestw){
    			no.w=top.w;
    			no.priority=top.w+r;
    			no.nexti=i+1;
    			memcpy(no.x,top.x,sizeof(top.x));
    			no.x[i]=0;
    			cout<<no.w<<" "<<no.priority<<" "<<no.nexti<<endl; 
    			Print(no,i);
    			q.push(no); 
    		}
    		top=q.top();
    		q.pop();
    		i=top.nexti;	
    	}	
    	cout<<"bestw="<<bestw<<endl; 
    	Print(no,n);	
    } 
    int main(){
    	cin>>c;
    	cin>>n;
    	for(int i=1;i<=n;i++){
    		cin>>w[i];	
    		r=r+w[i];
    	}	
    	cout<<"----------\n";
    	CalculateR();
    	MaxLoading();
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    在这里插入图片描述

  • 相关阅读:
    PHP下富文本HTML过滤器:HTMLPurifier使用教程
    单连通图的判断
    11.1 校招 实习 内推 面经
    分布式系分发展概览
    重构项目 vue2 => vue3 & nuxt2 => nuxt3 遇到的问题
    rust 初识基础: 变量、数据类型、函数、所有权、枚举
    Coze入门指南:创建Bot时,如何写好人设与回复逻辑(Persona & Prompt)
    CVE-2023-38831漏洞实例
    OSSID: Online Self-Supervised Instance Detection by (And For) Pose Estimation
    Vue3动态显示时间
  • 原文地址:https://blog.csdn.net/QMU111/article/details/128122993