使用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上. 下面给个计划 ...
随机推荐
- flex builder 4.7 ios 该溶液无法找到设备
行政建议,没有设备 1) 视图itunes 该服务正在启动(Bonjour服务).测试..... .即使没有这样的假设 2)"D:\Program Files\Adobe\Adobe Fla ...
- Redhat Linux挂载NTFS格式的移动硬盘
Redhat Linux挂载NTFS格式的移动硬盘 1. 选择下载ntfs-3g的源码包或rpm包 http://www.tuxera.com/community/open-source-ntfs-3 ...
- WebBrowser控件使用技巧分享
原文:WebBrowser控件使用技巧分享 在发布“淘宝登货员”时发现不少朋友对WebBrowser控件比较感兴趣,故在此分享一下使用心得. 首先分享一个WebBrowser的扩展类(此类所需的dll ...
- easyui datagrid 单元格编辑 即见即所得,MVC菜单维护,扫描增加
效果如图: 参见 EasyUI 官方 Demo 及文档 @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Lay ...
- .net中用Action等委托向外传递参数
原文:.net中用Action等委托向外传递参数 一般我们可以使用ref,out达到向外传递参数目的. Action<T>是一个特殊的委托,除了常规应用.我们还可以用它来实现简单 ...
- ActivityLifeCycle官方demo分解
1.左右Activity生命周期的若干条款: p=330">http://1.duoinfo.sinaapp.com/? p=330 http://1.duoinfo.sinaapp. ...
- [ASP.NET MVC]如何定制Numeric属性/字段验证消息
原文:[ASP.NET MVC]如何定制Numeric属性/字段验证消息 对于一个Numeric属性/字段,ASP.NET MVC会自动进行数据类型的验证(客户端验证),以确保输入的是一个有效的数字, ...
- 远程调用之RMI技术
---恢复内容开始--- RMI已经不是什么新的技术了,但是相对于webservice来说,rmi比较简单,比较适合一些小的应用,下面的helloword列子可以介绍rmi的相关技术 服务器端代码: ...
- linux下编译安装mysql5.5以上版本
安装cmake: tar zxvf cmake-2.8.4.tar.gz cd cmake-2.8.4 ./configure --prefix=/usr/local/cmake make & ...
- CSS中选择器优先级顺序实战讲解
原文:CSS中选择器优先级顺序实战讲解 我们有些程序猿在给一个元素(比如div)应用样式的时候,会有一些疑问,为什么我写在后面的样式不能覆盖前面的样式呢,不是说CSS是层叠样式表吗? 如果你在开发中也 ...