0034算法笔记分支限界法最优装载问题Word文档下载推荐.docx
《0034算法笔记分支限界法最优装载问题Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《0034算法笔记分支限界法最优装载问题Word文档下载推荐.docx(19页珍藏版)》请在冰豆网上搜索。
bool
IsEmpty()const{return
front==rear;
11.
IsFull(){return
(
(rear+1)
%MaxSize==front
)?
1:
0);
12.
T
Top()
const;
13.
Last()
14.
Queue<
&
Add(const
T&
x);
15.
AddLeft(const
16.
Delete(T
17.
void
Output(ostream&
out)const;
18.
int
Length(){return
(rear-front);
19.
private:
20.
front;
21.
rear;
22.
MaxSize;
23.
*queue;
24.};
25.
26.template<
27.Queue<
:
MaxQueueSize)
28.{
29.
MaxSize=MaxQueueSize+1;
30.
queue=new
T[MaxSize];
31.
front=rear=0;
32.}
33.
34.template<
>
35.T
Top()const
36.{
37.
if(IsEmpty())
38.
{
39.
cout<
"
queue:
no
element,no!
endl;
40.
return
0;
41.
42.
else
queue[(front+1)
%
MaxSize];
43.}
44.
45.template<
46.T
Last()const
47.{
48.
49.
50.
element"
51.
52.
53.
queue[rear];
54.}
55.
56.template<
57.Queue<
x)
58.{
59.
if(IsFull())cout<
memory"
60.
61.
62.
rear=(rear+1)%
63.
queue[rear]=x;
64.
65.
*this;
66.}
67.
68.template<
69.Queue<
70.{
71.
72.
73.
74.
front=(front+MaxSize-1)%
75.
queue[(front+1)%
MaxSize]=x;
76.
77.
78.}
79.
80.template<
81.Queue<
82.{
83.
if(IsEmpty())cout<
element(delete)"
84.
85.
86.
front=(front+1)
87.
x=queue[front];
88.
89.
90.}
91.
92.
93.template<
94.void
out)const
95.{
96.
for(int
i=rear%MaxSize;
i>
=(front+1)%MaxSize;
i--)
97.
out<
queue[i];
98.}
99.
100.template<
101.ostream&
operator
(ostream&
out,const
102.{x.Output(out);
out;
2、6d3-1.cpp
1.//装载问题
队列式分支限界法求解
2.#include
stdafx.h"
3.#include
Queue.h"
4.#include
5.using
6.
7.const
N
=
4;
9.template<
Type>
10.class
QNode
11.{
template<
friend
EnQueue(Queue<
QNode<
*>
Q,Type
wt,int
i,int
n,Type
bestw,QNode<
*E,QNode<
*&
bestE,int
bestx[],bool
ch);
Type
MaxLoading(Type
w[],Type
c,int
n,int
bestx[]);
*parent;
//指向父节点的指针
LChild;
//左儿子标识
weight;
//节点所相应的载重量
22.};
24.template<
25.void
26.
27.template<
28.Type
30.int
main()
31.{
32.
float
c
70;
w[]
{0,20,10,26,15};
//下标从1开始
34.
x[N+1];
35.
bestw;
36.
轮船载重为:
c<
待装物品的重量分别为:
i=1;
i<
=N;
i++)
w[i]<
;
43.
bestw
MaxLoading(w,c,N,x);
45.
46.
分支限界选择结果为:
47.
=4;
x[i]<
最优装载重量为:
bestw<
54.
55.}
56.
57.//将活节点加入到活节点队列Q中
58.template<
59.void
ch)
60.{
if(i
==
n)//可行叶节点
if(wt
bestw)
//当前最优装载重量
66.
bestE
E;
bestx[n]
ch;
68.
69.
return;
70.
//非叶节点
*b;
b
new
b->
weight
wt;
parent
LChild
Q.Add(b);
81.Type
bestx[])
82.{//队列式分支限界法,返回最优装载重量,bestx返回最优解
//初始化
Q;
//活节点队列
Q.Add(0);
//同层节点尾部标识
i
1;
//当前扩展节点所处的层
Ew
0,
//扩展节点所相应的载重量
r
//剩余集装箱重量
90.
j=2;
j<
=n;
j++)
93.
+=
w[j];
94.
95.
*E
//当前扩展节点
*bestE;
//当前最优扩展节点
98.
//搜索子集空间树
100.
while(true)
101.
102.
//检查左儿子节点
103.
wt
+
w[i];
104.
c)//可行节点
105.
106.
if(wt>
107.
108.
109.
110.
EnQueue(Q,wt,i,n,bestw,E,bestE,bestx,true);
111.
112.
113.
//检查右儿子节点
114.
if(Ew+r>
115.
116.
EnQueue(Q,Ew,i,n,bestw,E,bestE,bestx,false);
117.
118.
Q.Delete(E);
//取下一扩展节点
119.
120.
if(!
E)//同层节点尾部
121.
122.
if(Q.IsEmpty())
123.
124.
break;
125.
126.
127.
128.
i++;
//进入下一层
129.
r-=w[i];
130.
131.
=E->
//新扩展节点所对应的载重量
132.
133.
134.
//构造当前最优解
135.
j=n-1;
j>
j--)
136.
137.
bestx[j]
bestE->
138.
parent;
139.
140.
141.}
程序运行结果如图:
2、优先队列式分支限界法求解
解装载问题的优先队列式分支限界法用最大优先队列存储活结点表。
活结点x在优先队列中的优先级定义为从根结点到结点x的路径所相应的载重量再加上剩余集装箱的重量之和。
优先队列中优先级最大的活结点成为下一个扩展结点。
以结点x为根的子树中所有结点相应的路径的载重量不超过它的优先级。
子集树中叶结点所相应的载重量与其优先级相同。
在优先队列式分支限界法中,一旦有一个叶结点成为当前扩展结点,则可以断言该叶结点所相应的解即为最优解。
此时可终止算法。
1、MaxHeap.h
1.template<
2.class
MaxHeap
3.{
4.
5.
MaxHeap(int
MaxHeapSize
10);
~MaxHeap()
{delete
heap;
Size()
const
{return
CurrentSize;
Max()
//查
if
(CurrentSize
0)
throw
OutOfBounds();
heap[1];
MaxHeap<
Insert(const
//增
DeleteMax(T&
//删
Initialize(T
a[],
size,
ArraySize);
24.
CurrentSize,
*heap;
//
element
array
26.};
27.
28.template<
29.MaxHeap<
MaxHeapSize)
30.{//
Max
heap
constructor.
MaxSize
MaxHeapSize;
T[MaxSize+1];
CurrentSize
34.}
36.template<
37.MaxHeap<
38.{//
Insert
x
into
the
max
heap.
MaxSize)
space!
寻找新元素x的位置
i——初始为新叶节点的位置,逐层向上,寻找最终位置
++CurrentSize;
while
(i
!
1
heap[i/2])
i不是根节点,且其值大于父节点的值,需要继续调整
heap[i]
heap[i/2];
父节点下降
/=
2;
继续向上,搜寻正确位置
x;
57.}
58.
59.template<
60.MaxHeap<
61.{//
Set
to
and
delete
from
check
is
empty
Empty
heap!
删除最大元素
重整堆
y
heap[CurrentSize--];
取最后一个节点,从根开始重整
find
place
for
starting
at
root
1,
current
node
of
ci
child
(ci
CurrentSize)
78.
使ci指向i的两个孩子中较大者
80.
heap[ci]
heap