装载问题之分支限界法

    有一批共n个集装箱要装上2艘重量分别为c1和c2的轮船,其中集装箱i的重量为wi,满足w1+w2+...+wn<c1+c2。
    基本思路:尽可能将第一艘船装满,如果剩余的货物和小于等于第二艘船的容积,则可以放下,否则无法放下。
    目标:对第一艘船装载时的优化问题。

参考代码如下:
#include <stdio.h>
#include <stdlib.h>

typedef struct QNode{
	int data;
	struct QNode *next;
}QNode, *QueuePtr;

typedef struct{
	QueuePtr front;
	QueuePtr rear;
}Queue;

int initQueue(Queue &Q)
{
	Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
	if(!Q.front)
		return -1;
	Q.front->next=NULL;

	return 1;
}

int emptyQueue(Queue Q)
{
	if (Q.front==Q.rear)
		return 1;
	else
		return 0;
}

int destroyQueue(Queue &Q)
{
	while(Q.front){
		Q.rear=Q.front->next;
		free(Q.front);
		Q.front=Q.rear;
	}
	return 1;
}

int enQueue(Queue &Q, int ele)
{
	QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
	if(!p)
		return -1;
	p->data=ele; p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
	
	return 1;
}

int deQueue(Queue &Q, int &ele)
{
	QueuePtr p;
	if(Q.front==Q.rear)
		return -1;
	p=Q.front->next;
	ele=p->data;
	Q.front->next=p->next;

	if(Q.rear==p) Q.rear=Q.front;
	
	free(p);

	return 1;
}

Queue loadingQueue;
int bestw, n;

void inQueue(int wt, int i)
{
	if(i==n-1)
	{
		if(wt>bestw)
			bestw=wt;
	}
	else
		enQueue(loadingQueue,wt);
}



int main()
{
	
	int i,j,k;
	int *w, ew;
	int c;

	printf("input the number of things and the volume of ships:");
	scanf("%d%d",&n,&c);

	w=new int[n];
	printf("input the weights:");
	
	for(i=0;i<n;i++)
		scanf("%d",&w[i]);
	
	initQueue(loadingQueue);
	enQueue(loadingQueue,-1);

	i=0;	//层数
	ew=0;	//扩展结点对应的载重量

	while(true)
	{
		if(ew+w[i]<=c)
			inQueue(ew+w[i],i);
		inQueue(ew,i);

		deQueue(loadingQueue, ew);

		if(ew==-1) //同层结点尾部
		{
			if(emptyQueue(loadingQueue))
			{
				printf("the result is %d.\n",bestw);
			}

			enQueue(loadingQueue,-1);
			deQueue(loadingQueue, ew);
			i++;
		}
	}		
	
	return 0;
}






  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
#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; }
装载问题分支限界法(Branch and Bound)是一种用于求解组合优化问题的算法,特别适用于那些具有大量可能解和部分有序结构的搜索空间。在C++中实现装载问题分支限界法,比如旅行商问题(Traveling Salesman Problem, TSP)或车辆路径问题(Vehicle Routing Problem, VRP),通常会涉及以下步骤: 1. **定义问题**:明确装载物品的限制(如车辆容量、重量、体积等)、物品的起始位置以及目标是找到最短路径或最小成本。 2. **状态表示**:使用数组、vector或类似数据结构来表示当前装载状态,记录每个位置的装载情况。 3. **搜索树结构**:创建一个搜索树,根节点代表初始状态,子节点由可能的选择(比如选择一个未装载的物品或改变一个已装载物品的位置)产生。 4. **分支策略**:从每个节点生成一组可行的子节点,通常通过枚举可能的动作来实现。 5. **评估函数**:为每个节点计算一个估价函数,这个函数估计当前状态到最优解的代价,常见的有启发式函数(如欧几里得距离的总和)。 6. **剪枝策略**:利用上界(upper bound)和下界(lower bound)比较,如果发现某个分支不可能优于当前最优解,就提前结束搜索,避免无效探索。 7. **递归调用**:对每个子节点递归地应用分支限界法,直到达到叶子节点或达到预定的停止条件。 8. **回溯**:当找到最优解时,回溯搜索树以获取完整的解决方案路径。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值