c_cpp 有一批共个集装箱要装上2艘载重量分别为C1和C2的轮船,其中集装箱我的重量为无线,且装载问题要求确定是否有一个合理的装载方案可将这个集装箱装上这2艘轮船。找出一种装载方案。<br/&

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 有一批共个集装箱要装上2艘载重量分别为C1和C2的轮船,其中集装箱我的重量为无线,且装载问题要求确定是否有一个合理的装载方案可将这个集装箱装上这2艘轮船。找出一种装载方案。<br/&相关的知识,希望对你有一定的参考价值。

template<class T>
class MaxHeap
{
	public:
		MaxHeap(int MaxHeapSize = 10);
		~MaxHeap() {delete [] heap;}
        int Size() const {return CurrentSize;}
 
        T Max() 
		{          //查
           if (CurrentSize == 0)
		   {
                throw OutOfBounds();
		   }
           return heap[1];
        }
 
		MaxHeap<T>& Insert(const T& x); //增
		MaxHeap<T>& DeleteMax(T& x);   //删
 
		void Initialize(T a[], int size, int ArraySize);
 
	private:
		int CurrentSize, MaxSize;
		T *heap;  // element array
};
 
template<class T>
MaxHeap<T>::MaxHeap(int MaxHeapSize)
{// Max heap constructor.
	MaxSize = MaxHeapSize;
	heap = new T[MaxSize+1];
	CurrentSize = 0;
}
 
template<class T>
MaxHeap<T>& MaxHeap<T>::Insert(const T& x)
{// Insert x into the max heap.
	if (CurrentSize == MaxSize)
	{
		cout<<"no space!"<<endl; 
		return *this; 
	}
 
    // 寻找新元素x的位置
    // i——初始为新叶节点的位置,逐层向上,寻找最终位置
	int i = ++CurrentSize;
	while (i != 1 && x > heap[i/2])
	{
		// i不是根节点,且其值大于父节点的值,需要继续调整
		heap[i] = heap[i/2]; // 父节点下降
		i /= 2;              // 继续向上,搜寻正确位置
    }
 
   heap[i] = x;
   return *this;
}
 
template<class T>
MaxHeap<T>& MaxHeap<T>::DeleteMax(T& x)
{// Set x to max element and delete max element from heap.
	// check if heap is empty
	if (CurrentSize == 0)
	{
		cout<<"Empty heap!"<<endl; 
		return *this; 
	}
 
	x = heap[1]; // 删除最大元素
	// 重整堆
	T y = heap[CurrentSize--]; // 取最后一个节点,从根开始重整
 
	// find place for y starting at root
	int i = 1,  // current node of heap
	   ci = 2; // child of i
 
	while (ci <= CurrentSize) 
    {
		// 使ci指向i的两个孩子中较大者
		if (ci < CurrentSize && heap[ci] < heap[ci+1])
		{
			ci++;
		}
		// y的值大于等于孩子节点吗?
		if (y >= heap[ci])
		{
			break;   // 是,i就是y的正确位置,退出
		}
 
		// 否,需要继续向下,重整堆
		heap[i] = heap[ci]; // 大于父节点的孩子节点上升
		i = ci;             // 向下一层,继续搜索正确位置
		ci *= 2;
    }
 
	heap[i] = y;
	return *this;
}
 
template<class T>
void MaxHeap<T>::Initialize(T a[], int size,int ArraySize)
{// Initialize max heap to array a.
	delete [] heap;
	heap = a;
	CurrentSize = size;
	MaxSize = ArraySize;
 
	// 从最后一个内部节点开始,一直到根,对每个子树进行堆重整
   for (int i = CurrentSize/2; i >= 1; i--)
   {
		T y = heap[i]; // 子树根节点元素
		// find place to put y
		int c = 2*i; // parent of c is target
                   // location for y
		while (c <= CurrentSize) 
		{
			// heap[c] should be larger sibling
			if (c < CurrentSize && heap[c] < heap[c+1])
			{
				c++;
			}
			// can we put y in heap[c/2]?
			if (y >= heap[c])
			{
				break;  // yes
			}
 
			// no
			heap[c/2] = heap[c]; // move child up
			c *= 2; // move down a level
        }
		heap[c/2] = y;
	}
}
//装载问题 队列式分支限界法求解 
#include "stdafx.h"
#include "Queue.h"
#include <iostream>
using namespace std;
 
const int N = 4;
 
template<class Type>
class QNode
{
	template<class Type>
	friend void EnQueue(Queue<QNode<Type>*>&Q,Type wt,int i,int n,Type bestw,QNode<Type>*E,QNode<Type> *&bestE,int bestx[],bool ch);
 
	template<class Type>
	friend Type MaxLoading(Type w[],Type c,int n,int bestx[]);
 
	private:
		QNode *parent;	//指向父节点的指针
		bool LChild;    //左儿子标识
		Type weight;    //节点所相应的载重量
};
 
template<class Type>
void EnQueue(Queue<QNode<Type>*>&Q,Type wt,int i,int n,Type bestw,QNode<Type>*E,QNode<Type> *&bestE,int bestx[],bool ch);
 
template<class Type>
Type MaxLoading(Type w[],Type c,int n,int bestx[]);
 
int main()
{
	float c = 70;  
    float w[] = {0,20,10,26,15};//下标从1开始  
    int x[N+1];  
	float bestw;
  
    cout<<"轮船载重为:"<<c<<endl;  
    cout<<"待装物品的重量分别为:"<<endl;  
    for(int i=1; i<=N; i++)  
    {  
        cout<<w[i]<<" ";  
    }  
    cout<<endl;  
    bestw = MaxLoading(w,c,N,x);  
  
    cout<<"分支限界选择结果为:"<<endl;  
    for(int i=1; i<=4; i++)  
    {  
        cout<<x[i]<<" ";  
    }  
    cout<<endl;  
	cout<<"最优装载重量为:"<<bestw<<endl;
  
    return 0;  
}
 
//将活节点加入到活节点队列Q中
template<class Type>
void EnQueue(Queue<QNode<Type>*>&Q,Type wt,int i,int n,Type bestw,QNode<Type>*E,QNode<Type> *&bestE,int bestx[],bool ch)
{
	if(i == n)//可行叶节点
	{
		if(wt == bestw)
		{
			//当前最优装载重量
			bestE = E;
			bestx[n] = ch;			
		}
		return;
	}
	//非叶节点
	QNode<Type> *b;
	b = new QNode<Type>;
	b->weight = wt;
	b->parent = E;
	b->LChild = ch;
	Q.Add(b);
}
 
template<class Type>
Type MaxLoading(Type w[],Type c,int n,int bestx[])
{//队列式分支限界法,返回最优装载重量,bestx返回最优解
 //初始化
	Queue<QNode<Type>*> Q;		//活节点队列
	Q.Add(0);					//同层节点尾部标识
	int i = 1;					//当前扩展节点所处的层
	Type Ew = 0,				//扩展节点所相应的载重量
		 bestw = 0,				//当前最优装载重量
		 r = 0;					//剩余集装箱重量
 
	for(int j=2; j<=n; j++)
	{
		r += w[j];
	}
	
	QNode<Type> *E = 0,			//当前扩展节点
				*bestE;			//当前最优扩展节点
 
	//搜索子集空间树
	while(true)
	{
		//检查左儿子节点
		Type wt = Ew + w[i];
		if(wt <= c)//可行节点
		{
			if(wt>bestw)
			{
				bestw = wt;
			}
			EnQueue(Q,wt,i,n,bestw,E,bestE,bestx,true);
		}
 
		//检查右儿子节点
		if(Ew+r>bestw)
		{
			EnQueue(Q,Ew,i,n,bestw,E,bestE,bestx,false);
		}
		Q.Delete(E);//取下一扩展节点
 
		if(!E)//同层节点尾部
		{
			if(Q.IsEmpty())
			{
				break;
			}
			Q.Add(0);       //同层节点尾部标识
			Q.Delete(E);	//取下一扩展节点
			i++;			//进入下一层
			r-=w[i];		//剩余集装箱重量
		}
		Ew  =E->weight;		//新扩展节点所对应的载重量
	}
 
	//构造当前最优解
	for(int j=n-1; j>0; j--)
	{
		bestx[j] = bestE->LChild;
		bestE = bestE->parent;
	}
	return bestw;
}
//装载问题 优先队列式分支限界法求解 
#include "stdafx.h"
#include "MaxHeap.h"
#include <iostream>
using namespace std;
 
const int N = 4;
 
class bbnode;
 
template<class Type>
class HeapNode
{
	template<class Type>
	friend void AddLiveNode(MaxHeap<HeapNode<Type>>& H,bbnode *E,Type wt,bool ch,int lev);
	template<class Type>
	friend Type MaxLoading(Type w[],Type c,int n,int bestx[]);
	public:
		operator Type() const{return uweight;}
	private:
		bbnode *ptr;		//指向活节点在子集树中相应节点的指针
		Type uweight;		//活节点优先级(上界)
		int level;			//活节点在子集树中所处的层序号
};
 
class bbnode
{
	template<class Type>
	friend void AddLiveNode(MaxHeap<HeapNode<Type>>& H,bbnode *E,Type wt,bool ch,int lev);
	template<class Type>
	friend Type MaxLoading(Type w[],Type c,int n,int bestx[]);
	friend class AdjacencyGraph;
 
	private:
		bbnode *parent;		//指向父节点的指针
		bool LChild;		//左儿子节点标识
};
 
template<class Type>
void AddLiveNode(MaxHeap<HeapNode<Type>>& H,bbnode *E,Type wt,bool ch,int lev);
 
template<class Type>
Type MaxLoading(Type w[],Type c,int n,int bestx[]);
 
 
int main()
{
	float c = 70;  
    float w[] = {0,20,10,26,15};//下标从1开始  
    int x[N+1];  
	float bestw;
  
    cout<<"轮船载重为:"<<c<<endl;  
    cout<<"待装物品的重量分别为:"<<endl;  
    for(int i=1; i<=N; i++)  
    {  
        cout<<w[i]<<" ";  
    }  
    cout<<endl;  
    bestw = MaxLoading(w,c,N,x);  
  
    cout<<"分支限界选择结果为:"<<endl;  
    for(int i=1; i<=4; i++)  
    {  
        cout<<x[i]<<" ";  
    }  
    cout<<endl;  
	cout<<"最优装载重量为:"<<bestw<<endl;
  
    return 0; 
}
 
//将活节点加入到表示活节点优先队列的最大堆H中
template<class Type>
void AddLiveNode(MaxHeap<HeapNode<Type>>& H,bbnode *E,Type wt,bool ch,int lev)
{
	bbnode *b = new bbnode;
	b->parent = E;
	b->LChild = ch;
	HeapNode<Type> N;
 
	N.uweight = wt;
	N.level = lev;
	N.ptr = b;
	H.Insert(N);
}
 
//优先队列式分支限界法,返回最优载重量,bestx返回最优解
template<class Type>
Type MaxLoading(Type w[],Type c,int n,int bestx[])
{
	//定义最大的容量为1000
	MaxHeap<HeapNode<Type>> H(1000);
 
	//定义剩余容量数组
	Type *r = new Type[n+1];
	r[n] = 0;
 
	for(int j=n-1; j>0; j--)
	{
		r[j] = r[j+1] + w[j+1];
	}
 
	//初始化
	int i = 1;//当前扩展节点所处的层
	bbnode *E = 0;//当前扩展节点
	Type Ew = 0; //扩展节点所相应的载重量
 
	//搜索子集空间树
	while(i!=n+1)//非叶子节点
	{
		//检查当前扩展节点的儿子节点
		if(Ew+w[i]<=c)
		{
			AddLiveNode(H,E,Ew+w[i]+r[i],true,i+1);
		}
		//右儿子节点
		AddLiveNode(H,E,Ew+r[i],false,i+1);
 
		//取下一扩展节点
		HeapNode<Type> N;
		H.DeleteMax(N);//非空
		i = N.level;
		E = N.ptr;
		Ew = N.uweight - r[i-1];
	}
 
	//构造当前最优解
	for(int j=n; j>0; j--)
	{
		bestx[j] = E->LChild;
		E = E->parent;
	}
 
	return Ew;
}
#include<iostream>
using namespace std;
 
template <class T>
class Queue
{
	public:
		Queue(int MaxQueueSize=50);
		~Queue(){delete [] queue;}
		bool IsEmpty()const{return front==rear;}
		bool IsFull(){return ( (  (rear+1)  %MaxSize==front )?1:0);}
		T Top() const;
		T Last() const;
		Queue<T>& Add(const T& x);
		Queue<T>& AddLeft(const T& x);
		Queue<T>& Delete(T &x);
		void Output(ostream& out)const;
		int Length(){return (rear-front);}
	private:
		int front;
		int rear;
		int MaxSize;
		T *queue;
};
 
template<class T>
Queue<T>::Queue(int MaxQueueSize)
{
	MaxSize=MaxQueueSize+1;
	queue=new T[MaxSize];
	front=rear=0;
}
 
template<class T >
T Queue<T>::Top()const
{
	if(IsEmpty())
	{
		cout<<"queue:no element,no!"<<endl;
		return 0;
	}
	else return queue[(front+1) % MaxSize];
}
 
template<class T>
T Queue<T> ::Last()const
{
	if(IsEmpty())
	{
		cout<<"queue:no element"<<endl;
		return 0;
	}
	else return queue[rear];
}
 
template<class T>
Queue<T>&  Queue<T>::Add(const T& x)
{
	if(IsFull())cout<<"queue:no memory"<<endl;
	else
	{
		rear=(rear+1)% MaxSize;
	    queue[rear]=x;
	}
	return *this;
}
 
template<class T>
Queue<T>&  Queue<T>::AddLeft(const T& x)
{
	if(IsFull())cout<<"queue:no memory"<<endl;
	else
	{
		front=(front+MaxSize-1)% MaxSize;
		queue[(front+1)% MaxSize]=x;
	}
	return *this;
}
 
template<class T>
Queue<T>&  Queue<T> ::Delete(T & x)
{
	if(IsEmpty())cout<<"queue:no element(delete)"<<endl;
	else 
	{
		front=(front+1) % MaxSize;
		x=queue[front];
	}
	return *this;
}
 
 
template<class T>
void Queue <T>::Output(ostream& out)const
{
	for(int i=rear%MaxSize;i>=(front+1)%MaxSize;i--)
	   out<<queue[i];
}
 
template<class T>
ostream& operator << (ostream& out,const Queue<T>& x)
{x.Output(out);return out;}

以上是关于c_cpp 有一批共个集装箱要装上2艘载重量分别为C1和C2的轮船,其中集装箱我的重量为无线,且装载问题要求确定是否有一个合理的装载方案可将这个集装箱装上这2艘轮船。找出一种装载方案。<br/&的主要内容,如果未能解决你的问题,请参考以下文章

回溯算法 - 最优装载

装载问题回溯法

尽可能装满的背包问题

装载问题,0-1背包

最优装载(贪心)

最优装载