#include"d_random.h"
#include"d_timer.h"
#include<iostream>
#include<queue>
using namespace std;
struct bbnode
{
bbnode *parent;
int LChild;
};
class HeapNode
{
public :
friend bool operator<(const HeapNode& a, const HeapNode& b)
{
return (a.uweight<b.uweight);
}
bbnode *ptr;
int uweight;
int level;
};
void AddLiveNode(priority_queue<HeapNode> &H,bbnode* E,int wt,int ch,int lev)
{
bbnode* b=new bbnode;
b->parent=E;
b->LChild=ch;
HeapNode N;
N.uweight=wt;
N.level=lev;
N.ptr=b;
H.push(N);
}
int MaxLoading (int w[],int c,int n,int bestx[])
{
priority_queue<HeapNode> H;
int* r=new int[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;
int Ew=0;
while(i!=n+1)
{
int wt=Ew+w[i];
if(wt<=c)
{
AddLiveNode(H,E,wt+r[i],1,i+1);
}
AddLiveNode(H,E,Ew+r[i],0,i+1);
}
HeapNode N;
N=H.top();
H.pop();
i=N.level;
E=N.ptr;
Ew=N.uweight-r[i-1];
for(j=n;j>0;j--){
bestx[j]=E->LChild;
E=E->parent;
}
return Ew;
}
int main()
{
timer t1;//定义时间
int bestx[100];//记录最优装载
int count=0;
int weight;//重量
int i;//定义变量i
int n=3;//定义25个集装箱
int c[2];//定义2艘船
int w[4]={0,10,40,40};//定义25个集装箱的重量,除掉w[0]
randomNumber ran;//随机数
for(i=0;i<2;i++)//船的载重初始化为1000
{
c[i]=50;
}
for(i=0;i<=n;i++)
{
bestx[i]=0;//初始化
}
w[0]=0;
/* for(i=1;i<=n;i++)
{//随即生成n个集装箱的重量
w[i]=ran.random(40)+1;
}*/
t1.start();
weight=MaxLoading(w,c[0],n,bestx);
t1.stop();
cout<<"当前最优载重为:"<<weight<<endl;
cout<<"输出"<<n<<"集装箱的重量:"<<endl;
for(i=0;i<n;i++)
{
if(i%10==0&&i!=0)
cout<<endl;
cout<<w[i+1]<<" ";
}
cout<<"所用的时间为:"<<t1.time()<<endl;
for(i=1;i<=n;i++){
if(bestx[i]==0)
{
count+=w[i];
}
}
if(count<=c[1]){
cout<<"货物装载情况(按排序后的集装箱顺序):"<<endl;;
for(i=1;i<=n;i++){
if(bestx[i]==0)
cout<<"货物"<<i<<"装载于第2艘船!"<<endl;
else
cout<<"货物"<<i<<"装载于第1艘船!"<<endl;
}
}
else{
cout<<"无法装载!";
}
cout<<endl;
return 0;
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
采用队列式分支限界法 在算法的while循环中,首先检测当前扩展结点的左儿子结点是否为可行结点。如果是则将其加入到活结点队列中。然后将其右儿子结点加入到活结点队列中(右儿子结点一定是可行结点)。2个儿子结点都产生后,当前扩展结点被舍弃。 活结点队列中的队首元素被取出作为当前扩展结点,由于队列中每一层结点之后都有一个尾部标记-1,故在取队首元素时,活结点队列一定不空。当取出的元素是-1时,再判断当前队列是否为空。如果队列非空,则将尾部标记-1加入活结点队列,算法开始处理下一层的活结点。
资源推荐
资源详情
资源评论
收起资源包目录
优先队列分支界限法.rar (16个子文件)
优先队列分支界限法
d_heap.h 4KB
优先队列分支界限法.dsw 544B
优先队列分支界限法.dsp 4KB
main2.cpp 2KB
优先队列分支界限法.ncb 65KB
Debug
vc60.pdb 132KB
vc60.idb 89KB
优先队列分支界限法.pch 1.92MB
main2.obj 188KB
优先队列分支界限法.pdb 1.06MB
优先队列分支界限法.ilk 774KB
优先队列分支界限法.exe 532KB
优先队列分支界限法.plg 270B
优先队列分支界限法.opt 48KB
d_random.h 1KB
d_timer.h 1KB
共 16 条
- 1
资源评论
- 小风的IT乐园2011-12-03资源很好,源代码没什么错误,下载下来基本可以运行了
- sunting1222011-12-06代码很全很管用,里面注释也很仔细。
- joe_chen_via2012-06-08还可以把,就是分支不明显,有点回溯的嫌疑
- feng8807152016-04-27分支部分那个地方有点模糊
liaoyuan11
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功