使用STL处理分支限界法处理最优装载问题
使用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处理分支限界法处理最优装载问题的更多相关文章
- 最优装载—dp
最优装载—dp 动态规划 一 问题描述 二 问题分析 三 代码实现 package dp_Loading; import java.io.BufferedWriter; import java.io. ...
- 回溯法最优装载问题(java)
1.问题描述: 有一批共有 n 个集装箱要装上两艘载重量分别为 c1 和 c2 的轮船,其中集装箱 i 的重量为 w[i], 且重量之和小于(c1 + c2).装载问题要求确定是否存在一个合 ...
- 装载问题(load)
装载问题(load) 问题描述: 有一批共n 个集装箱要装上艘载重量为c 的轮船,其中集装箱i 的重量为wi.找出一种最 优装载方案,将轮船尽可能装满,即在装载体积不受限制的情况下,将尽可能重的集装箱 ...
- (Java实现) 装载问题
2.装载问题 [问题描述] 有一批共n个集装箱要装上艘载重量为c的轮船,其中集装箱i的重量为wi.找出一种最优装载方案,将轮船尽可能装满,即在装载体积不受限制的情况下,将尽可能重的集装箱装上轮船. [ ...
- poj 题目分类(2)
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj329 ...
- ACM学习
转:ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. US ...
- ACM课程学习总结
ACM课程学习总结报告 通过一个学期的ACM课程的学习,我学习了到了许多算法方面的知识,感受到了算法知识的精彩与博大,以及算法在解决问题时的巨大作用.此篇ACM课程学习总结报告将从以下方面展开: 学习 ...
- (转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!
ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO ht ...
- 另一个ACM之路建议
ACM联系建议 一位高手对我的建议: 一般要做到50行以内的程序不用调试.100行以内的二分钟内调试成功.acm主要是考算法的 ,主要时间是花在思考算法上,不是花在写程序与debug上. 下面给个计划 ...
随机推荐
- 通过私有协议Chrome浏览器页面打开本地程序
近期方有这样的要求:这两个系统,根据一组Chrome开展,根据一组IE开展,需要Chrome添加一个链接,然后进入IE该系统的开发.这,需要Chrome跳转到创建一个链接IE浏览器指定的页面.同时也实 ...
- BZOJ 3209 花神的数论题 数位DP+数论
题目大意:令Sum(i)为i在二进制下1的个数 求∏(1<=i<=n)Sum(i) 一道非常easy的数位DP 首先我们打表打出组合数 然后利用数位DP统计出二进制下1的个数为x的数的数量 ...
- C#5.0新特性
C#5.0新特性 C#5.0最大的新特性,莫过于Async和Parallel. 以往我们为了让用户界面保持相应,我们可以直接使用异步委托或是System.Threading命名空间中的成员,但Syst ...
- linux内核源码目录(转)
Linux用来支持各种体系结构的源代码包含大约4500个C语言程序,存放在270个左右的子目录下,总共大约包含200万行代码,大概占用58MB磁盘空间. 源代码所有在目录:/usr/src/linux ...
- 2014阿里实习生面试题——mysql如何实现的索引
这是2014北京站的两副面孔阿里实习生问题扯在一起: 在MySQL中.索引属于存储引擎级别的概念,不同存储引擎对索引的实现方式是不同的,比方MyISAM和InnoDB存储引擎. MyISAM索引实现: ...
- Windows下结束tomcat进程,dos命令
Microsoft Windows [版本 6.1.7601]版权所有 (c) 2009 Microsoft Corporation.保留所有权利. C:\Users\Administrator> ...
- CSS代码实现图片防盗链
CSS代码实现图片防盗链的方法其实很简单.在CSS文件中添加以下代码: img { filter:exPRession( this.不能去掉 ? "" : ( (!this.com ...
- Spring源深和六系列 CreateBean过程
blog宗旨:用图说话. 这一章的图讲述了createBean的过程.到这里spring容器就能够完毕IOC的整个过程,拿到我们须要的对象. 下一章我们接着来看一看AOP完毕的过程. 附:文件夹 Sp ...
- WTIR Updating Page
NO REPLY. ############### #14090704# ###############
- Liunx readlink命令
readlink命令 分类: Shell 2013-07-13 16:41 417人阅读 评论(0) 收藏 举报 readlink是linux系统中一个常用工具,主要用来找出符号链接所指向的位置. 在 ...