使用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上. 下面给个计划 ...
随机推荐
- WebService它CXF注释错误(两)
WebService它CXF注解 1.详细报错例如以下 五月 04, 2014 11:24:12 下午 org.apache.cxf.wsdl.service.factory.ReflectionSe ...
- HDU 5068 Harry And Math Teacher
主题链接~~> 做题情绪:的非常高深,有种高大上的感觉. 解题思路: 两层之间的联通能够看成是一个矩阵 代表上下两层都能够联通,,代表下层第1个门与上层第一个门不联通,以此类推联通就能够用矩阵 ...
- linux添加静态路由表,重新启动继续有效
在日常使用中.要么server于.有两个地址,两块网卡的配置,访问不同网段.这样的情况是非常普遍的现象.但,我们需要添加到路由表中的一个额外的,以确定通过正确的网关发送的数据包,并interface能 ...
- HDU 1501 Zipper(DP,DFS)
意甲冠军 是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法 DP或者DFS 考虑DP 令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...
- 网站静态化处理—web前端优化—中(12)
网站静态化处理—web前端优化—中(12) Web前端很多优化原则都是从如何提升网络通讯效率的角度提出的,但是这些原则使用的时候还是有很多陷阱在里面,如果我们不能深入理解这些优化原则背后所隐藏的技术原 ...
- JS模块与命名空间的介绍二
区别一:
- C# 字符串加密解密函数
原文:C# 字符串加密解密函数 using System; using System.Text;using System.Security.Cryptography; using System.IO; ...
- SQL点滴19—T-SQL中的透视和逆透视
原文:SQL点滴19-T-SQL中的透视和逆透视 透视 今天抽一点时间来看看透视和逆透视语句,简单的说就是行列转换.假设一个销售表中存放着产品号,产品折扣,产品价格三个列,每一种产品号可能有多种折扣, ...
- lua及luci学习
由于项目需要对Luci进行修改,所以这里开始地luci进行较深入的研究. 探索其中的运行路径. Openwrt默认的HTTP服务器为uhttpd,该WEB服务器是由Luci的开发者自行开发的,非常小巧 ...
- jquery无缝滚动效果实现
demo如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...