使用STL处理分支限界法处理最优装载问题

#include <iostream>
#include <vector>
#include <queue>
#include <time.h>
#define MAX_SIZE 100
int SIZE;
using namespace std;
float Object_Weight[MAX_SIZE];
float SUM;
class Node{
public:
float total_weight;
int level;
Node(){
total_weight = 0;
level = 0;
for(int i=0;i<SIZE;i++)
result[i] = false;
}
Node(const Node& obj){
total_weight = obj.total_weight;
level = obj.level;
for(int i=0;i<SIZE;i++)
result[i] = obj.result[i];
}
Node& operator = (const Node &obj){
total_weight = obj.total_weight;
level = obj.level;
for(int i=0;i<SIZE;i++)
result[i] = obj.result[i];
return *this;
}
void set(bool value){
result[level-1] = value;
total_weight = getWeight();
}
float returnWeight(){return total_weight;}
float maxEstWeight();

void CopyResult(bool* re);
private:
float getWeight();
bool result[MAX_SIZE];
};
struct cmp{
bool operator()(Node& obj1, Node& obj2){
return obj1.total_weight<obj2.total_weight;
}
};
void Node::CopyResult(bool* re){
for(int i=0;i<SIZE;i++)
re[i] = result[i];
}
float Node::getWeight(){
float sum = 0;
for(int i=0;i<level;i++)
{
if(result[i])
sum += Object_Weight[i];
}
return sum;
}

float Node::maxEstWeight(){
float sum = total_weight;
for(int i=level;i<SIZE;i++)
sum += Object_Weight[i];
return sum;
}
void naiveMethod(float c1,float c2){
float bestWeight = 0;
int counter = 0;
bool* bestResult = new bool(SIZE);
vector<Node> Queue;
Node *a = new Node();
Queue.push_back(*a);
while(Queue.size() != 0){
Node temp(Queue[0]);
Queue.erase(Queue.begin());
if(temp.level != SIZE){
Node left(temp);
Node right(temp);
left.level++;
left.set(false);
right.level++;
right.set(true);
Queue.push_back(left);
Queue.push_back(right);
counter += 2;
}
if( (bestWeight < temp.returnWeight()) && (temp.returnWeight()<=c1) ){
bestWeight = temp.returnWeight();
temp.CopyResult(bestResult);
}
}//while
cout<<"c1 loading result:"<<bestWeight<<endl;
for(int i=0;i<SIZE;i++)
cout<< bestResult[i]<<" ";
cout<<endl;
cout<<"c2 loading result:"<<SUM-bestWeight<<endl;
for(int i=0;i<SIZE;i++)
cout<<! bestResult[i]<<" ";
cout<<endl;
cout<<"Total counter: "<<counter<<endl;
}

void queueMethod(int c1, int c2){
float bestWeight = 0;
int counter = 0;
bool* bestResult = new bool(SIZE);
vector<Node> Queue;
Node *a = new Node();
Queue.push_back(*a);
while(Queue.size() != 0){
Node temp(Queue[0]);
Queue.erase(Queue.begin());
if( (temp.level != SIZE) && (bestWeight < temp.maxEstWeight() ) ){
Node left(temp);
Node right(temp);
left.level++;
left.set(false);
right.level++;
right.set(true);
Queue.push_back(left);
Queue.push_back(right);
counter += 2;
}
if( (bestWeight < temp.returnWeight()) && (temp.returnWeight()<=c1) ){
bestWeight = temp.returnWeight();
temp.CopyResult(bestResult);
}
}//while
cout<<"c1 loading result:"<<bestWeight<<endl;
for(int i=0;i<SIZE;i++)
cout<< bestResult[i]<<" ";
cout<<endl;
cout<<"c2 loading result:"<<SUM-bestWeight<<endl;
for(int i=0;i<SIZE;i++)
cout<<! bestResult[i]<<" ";
cout<<endl;
cout<<"Total counter: "<<counter<<endl;
}

void priority_QueueMethod(int c1, int c2){
float bestWeight = 0;
int counter = 0;
bool* bestResult = new bool(SIZE);
priority_queue<Node, vector<Node>, cmp> Queue;
Node *a = new Node();
Queue.push(*a);
while(Queue.size() != 0){
Node temp(Queue.top());
Queue.pop();
if( (temp.level != SIZE) && (bestWeight < temp.maxEstWeight() ) ){
Node left(temp);
Node right(temp);
left.level++;
left.set(false);
right.level++;
right.set(true);
Queue.push(left);
Queue.push(right);
counter += 2;
}
if( (bestWeight < temp.returnWeight()) && (temp.returnWeight()<=c1) ){
bestWeight = temp.returnWeight();
temp.CopyResult(bestResult);
}
}//while
cout<<"c1 loading result:"<<bestWeight<<endl;
for(int i=0;i<SIZE;i++)
cout<< bestResult[i]<<" ";
cout<<endl;
cout<<"c2 loading result:"<<SUM-bestWeight<<endl;
for(int i=0;i<SIZE;i++)
cout<<! bestResult[i]<<" ";
cout<<endl;
cout<<"Total counter: "<<counter<<endl;
}

int main(){
float c1,c2;
SUM= 0;
cout<<"SIZE:"<<endl;
cin>>SIZE;
cout<<"WEIGHT:"<<endl;
for(int i=0;i<SIZE;i++){
cin>>Object_Weight[i];
SUM += Object_Weight[i];
}
cout<<"C1:"<<endl;
cin>>c1;
cout<<"C2:"<<endl;
cin>>c2;
if(c1+c2<SUM)
{
cout<<"No solution!"<<endl;
return EXIT_SUCCESS;
}
if(SUM<c1 || SUM<c2)
{
cout<<"Need only one ship!"<<endl;
return EXIT_SUCCESS;
}
time_t start ,end ;
double cost;
start = clock();
naiveMethod(c1, c2);
end = clock();
cost=difftime(end,start);
cout<<"///////////////\nNaive method time: "<<cost<<"\n///////////////"<<endl;
start = clock();
queueMethod(c1,c2);
end = clock();
cost=difftime(end,start);
cout<<"///////////////\nQueue method time: "<<cost<<"\n///////////////"<<endl;
start = clock();
priority_QueueMethod(c1,c2);
end = clock();
cost=difftime(end,start);
cout<<"///////////////\nPriority queue method time: "<<cost<<"\n///////////////"<<endl;
return EXIT_SUCCESS;
}

使用STL处理分支限界法处理最优装载问题的更多相关文章

  1. 最优装载—dp

    最优装载—dp 动态规划 一 问题描述 二 问题分析 三 代码实现 package dp_Loading; import java.io.BufferedWriter; import java.io. ...

  2. 回溯法最优装载问题(java)

    1.问题描述:      有一批共有 n 个集装箱要装上两艘载重量分别为 c1 和 c2 的轮船,其中集装箱 i 的重量为 w[i], 且重量之和小于(c1 + c2).装载问题要求确定是否存在一个合 ...

  3. 装载问题(load)

    装载问题(load) 问题描述: 有一批共n 个集装箱要装上艘载重量为c 的轮船,其中集装箱i 的重量为wi.找出一种最 优装载方案,将轮船尽可能装满,即在装载体积不受限制的情况下,将尽可能重的集装箱 ...

  4. (Java实现) 装载问题

    2.装载问题 [问题描述] 有一批共n个集装箱要装上艘载重量为c的轮船,其中集装箱i的重量为wi.找出一种最优装载方案,将轮船尽可能装满,即在装载体积不受限制的情况下,将尽可能重的集装箱装上轮船. [ ...

  5. poj 题目分类(2)

    初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj329 ...

  6. ACM学习

    转:ACM大量习题题库   ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库.   US ...

  7. ACM课程学习总结

    ACM课程学习总结报告 通过一个学期的ACM课程的学习,我学习了到了许多算法方面的知识,感受到了算法知识的精彩与博大,以及算法在解决问题时的巨大作用.此篇ACM课程学习总结报告将从以下方面展开: 学习 ...

  8. (转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!

    ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO ht ...

  9. 另一个ACM之路建议

    ACM联系建议 一位高手对我的建议: 一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的 ,主要时间是花在思考算法上,不是花在写程序与debug上. 下面给个计划 ...

随机推荐

  1. 【Android UI设计和开发】动画(Animation)详细说明(一)

    Android开发之动画效果浅析 请尊重他人的劳动成果.转载请注明出处:Android开发之动画效果浅析 程序执行效果图: Android动画主要包括补间动画(Tween)View Animation ...

  2. 【百度地图API】多家地图API内存消耗对比测验(带源码)

    原文:[百度地图API]多家地图API内存消耗对比测验(带源码) 任务描述: 啊,美妙的春节结束了.酸奶小妹和妈妈的山西平遥之旅也宣告成功!距离平遥古城7km,有一个同样身为“世界文化遗产”的寺庙,叫 ...

  3. 讨论IT选定的技术招聘企业几点

    在实际的招聘总结的几点思考,企业需要怎么样的人才,例如,以下个人总结: 1.技术能力是不是第一次 企业的时候,你往往看第一点的人的招聘是不是技术实力.但是,你的个人言论的行为和态度.能在半个小时内把你 ...

  4. [CLR via C#]1.3 加载公共语言运行时

    原文:[CLR via C#]1.3 加载公共语言运行时 1. 你生成的每个程序集可以是EXE,也可以是DLL.最终都是有CLR管理这些程序集中代码的执行. 2. VS2010中,创建新的EXE项目时 ...

  5. C#中四个判等函数的认识

    donet提供了四个判等函数,分别是referenceEqual,静态Equal,具体类型Equal,和==. 首先来说,object.referenceEqual和静态Equal public st ...

  6. github basic usage in windows

    1. create a new accout, create orginazation, create repo 2. install git in your local pc Note: you c ...

  7. CSS下背景属性background的使用方法

    背景颜色(background-color) CSS可以用纯色来作为背景,也可以将背景设置为透明,background相当于xhtml中的bgcolor. 它的两个值: transparent(默认值 ...

  8. ORACLE PL/SQL编程之八:把触发器说透

    原文:ORACLE PL/SQL编程之八:把触发器说透 ORACLE PL/SQL编程之八: 把触发器说透 大家一定要评论呀,感谢!光发表就花了我将近一个下午. 本篇主要内容如下: 8.1 触发器类型 ...

  9. MvcPager分页控件以适用Bootstrap

    随笔- 9  文章- 0  评论- 33  修改MvcPager分页控件以适用Bootstrap 效果(含英文版,可下载)   软件开发分页效果必不可少,对于Asp.Net MVC 而言,MvcPag ...

  10. OpenSUSE 13.2使用VPN(PPTP)

    新年开始,有时查询个资料或是下个软件包并不是那么愉快,决定使用付费VPN,他们使用的是用户名及密码的验证方式 在网上找到了一个教程,挺详尽的,如果想按照步骤能使用即可的原则,跟着我一起设置,想了解更多 ...