使用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. python_random随机

    在数据清洗,评估 ,抽验等等过程中,经常有这样的应用场景 : 需要在一个大的数据集合中随机出来样本,进行人工评估.为了保证足够随机,借助脚本来实现. 下面一个脚本  ,用于应对这种应用场景. 使用方法 ...

  2. Spring中的事务传播行为

    Spring在TransactionDefinition接口中定义了7种类型的事务传播行为,它们规定了事务方法是怎样传播的. PROPAGATION_REQUIRED(最经常使用!):支持当前事务,假 ...

  3. Robot Framework + appium 启动手机浏览器的两个方法(1)

    一.Open Browser启动 使用Selenium2Library的Open Browser方法,例子如下: browser=手机浏览器类型,如chrome 二.Open Application启 ...

  4. HDU 1385 Minimum Transport Cost 最短路径题解

    本题就是使用Floyd算法求全部路径的最短路径,并且须要保存路径,并且更进一步须要依照字典顺序输出结果. 还是有一定难度的. Floyd有一种非常巧妙的记录数据的方法,大多都是使用这种方法记录数据的. ...

  5. 关于Java中List对象的分页思想,按10个或者n个数对list进行分组

    try { List<String> timelist = DateUtils.getDateListBySETime("2015-08-01", "2015 ...

  6. Jquery AJAX POST与GET之间的区别

    1:GET访问 浏览器 认为 是等幂的就是 一个相同的URL 只有一个结果[相同是指 整个URL字符串完全匹配]所以 第二次访问的时候 如果 URL字符串没变化 浏览器是 直接拿出了第一次访问的结果 ...

  7. SQL点滴17—使用数据库引擎存储过程,系统视图查询,DBA,BI开发人员必备基础知识

    原文:SQL点滴17-使用数据库引擎存储过程,系统视图查询,DBA,BI开发人员必备基础知识 在开发过程中会遇到需要弄清楚这个数据库什么时候建的,这个数据库中有多少表,这个存储过程长的什么样子等等信息 ...

  8. Codeforces 429 A. Xor-tree

    下来的第一次相遇是在不翻盖的同一节点,递归可以是.... A. Xor-tree time limit per test 1 second memory limit per test 256 mega ...

  9. ECharts图表系统 特性总览

    最近在玩ECharts,感觉真心不错,在这里把官方的资料收集收集,给大家推荐一下下~ Architecture ECharts (Enterprise Charts 商业产品图表库) 提供商业产品常用 ...

  10. C语言身份证信息查询系统(修改版)

    很久以前写了一个<C语言身份证信息查询系统>,如果你点击链接进去看了. 估计也会被我那磅礴大气的代码震惊到的,最近复习/学习文件操作,把代码改了改,算是对以前还不会文件操作的时候的愿望,哈 ...