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

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<iostream> 
#include<queue> 
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;
}

四、优先队列分支限界法

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

//6-3装载问题 优先队列分支限界
//优先队列分支限界法  
/*
10 3
3 4 5
*/
#include<iostream> 
#include<queue> 
#include<cstring>
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;
}

在这里插入图片描述

  • 5
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
#include #include #include #include using namespace std; ifstream infile; ofstream outfile; class Node { friend int func(int*, int, int, int*); public: int ID; double weight;//物品的重量 }; bool comp1(Node a, Node b) //定义比较规则 { return a.weight > b.weight; } class Load; class bbnode; class Current { friend Load; friend struct Comp2; private: int upweight;//重量上界 int weight;//结点相应的重量 int level;//活结点在子集树中所处的层次 bbnode* ptr;//指向活结点在子集树中相应结点的指针 }; struct Comp2 { bool operator () (Current *x, Current *y) { return x->upweightupweight; } }; class Load { friend int func(int*, int, int, int*); public: int Max0(); private: priority_queue<Current*, vector, Comp2>H;//利用优先队列(最大堆)储存 int limit(int i); void AddLiveNode(int up, int cw, bool ch, int level); bbnode *P;//指向扩展结点的指针 int c;//背包的容量 int n;//物品的数目 int *w;//重量数组 int cw;//当前装载量 int *bestx;//最优解方案数组 }; class bbnode { friend Load; friend int func( int*, int, int, int*); bbnode* parent; bool lchild; }; //结点中有双亲指针以及左儿子标志 int Load::limit(int i) //计算结点所相应重量的上界 { int left,a; left= c - cw;//剩余容量 a = cw; //b是重量上界,初始值为已经得到的重量 while (i <= n && w[i] parent = P; b->lchild = ch; Current* N = new Current; N->upweight = up; N->weight = cw; N->level = level; N->ptr = b; H.push(N); } int Load::Max0() { int i = 1; P = 0; cw = 0; int bestw = 0; int up = limit(1); while (i != n + 1) { int wt = cw + w[i]; //检查当前扩展结点的左儿子结点 if (wt bestw) bestw =wt; AddLiveNode(up,wt, true, i + 1); } up = limit(i + 1); //检查当前扩展结点的右儿子结点 if (up >= bestw)//如果右儿子可行 { AddLiveNode(up,cw, false, i + 1); } Current* N = H.top(); //取队头元素 H.pop(); P = N->ptr; cw = N->weight; up = N->upweight; i = N->level; } bestx = new int[n + 1]; for (int j = n; j > 0; --j) { bestx[j] = P->lchild; P = P->parent; } return cw; } int func(int *w, int c, int n, int *bestx) //调用Max0函数对子集树的优先队列式进行分支限界搜索 { int W = 0; //初始化装载的总质量为0 Node* Q = new Node[n]; for (int i = 0; i < n; ++i) { Q[i].ID = i + 1; Q[i].weight = w[i+1]; W += w[i+1]; } if (W <= c)//如果足够装,全部装入 return W; sort(Q, Q + n, comp1); //首先,将各物品按照重量从大到小进行排序; Load K; K.w = new int[n + 1]; for (int j = 0; j < n; j++) K.w[j + 1] = w[Q[j].ID]; K.cw = 0; K.c = c; K.n = n; int bestp = K.Max0(); for (int k = 0; k < n; k++) { bestx[Q[k].ID] = K.bestx[k + 1]; } delete []Q; delete []K.w; delete []K.bestx; return bestp; } int main() { int*w,*Final; int c,n,i,best; infile.open("input.txt",ios::in); if(!infile) { cerr<<"open error"<>c; infile>>n; w=new int[n+1]; for(i=1;i>w[i]; infile.close(); Final = new int[n+1]; best = func( w, c, n, Final); outfile.open("output.txt",ios::out); if(!outfile) { cerr<<"open error"<<endl; exit(1); } outfile << best << endl; for (int i = 1; i <= n; ++i) { outfile<<Final[i]<<" "; } outfile.close(); return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清木!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值