回溯法解决装载问题
2011-12-05 21:43
288 查看
#include<stdio.h>
#include<iostream>
using namespace std;
typedef int* pointer;
template<class Type>
class Loding{
friend Type MaxLoding(Type [],Type,int,int[]);
private:
void Backtrack(int i);
int n,//集装箱数
*x,//当前解
*bestx;//当前最优解
Type *w,//集装箱重量数组
c,//第一艘船的载重量
cw,//当前载重量
bestw,//当前最优载重量
r;//剩余集装箱重量
};
template<class Type>
void Loding<Type>::Backtrack(int i)
{//搜索第i层节点
if(i > n){//到达叶节点
if(cw > bestw){
for(int j = 1;j <= n;j++)
bestx[j] = x[j];
bestw = cw;
}
return;
}
//搜索子树
r -= w[i];
if(cw + w[i] <= c){//搜索左子树
x[i] = 1;
cw += w[i];
Backtrack(i + 1);
cw -= w[i];
}
if(cw + r > bestw){//搜索右子树
x[i] = 0;
Backtrack(i+1);
}
r += w[i];
}
template<class Type>
Type MaxLoding(Type w[],Type c,int n,int bestx[]){
//返回最优载重量
Loding<Type> X;
//初始化X
X.x = (int*)malloc( (n+1) * sizeof(int) );
X.w = w;
X.c = c;
X.n = n;
X.bestx = bestx;
X.bestw = 0;
X.cw = 0;
//初始化r
X.r = 0;
for(int i = 1;i <= n;i++)
X.r += w[i];
X.Backtrack(1);
delete []X.x;
return X.bestw;
}
template<class Type>
void solveLoading(Type C2,int bestx[],int n,Type w[]){
int Weight = 0;//集装箱的总重量
int weightC1 = 0;//第一艘船上的集装箱的重量和
for(int i = 1;i <= n;i++){
Weight += w[i];
if(bestx[i]==1)
weightC1 += w[i];
}
if((Weight - weightC1) > C2)
printf("该装载问题无解!\n");
else
{
printf("装在第一艘船上的集装箱为:");
for(int i = 1;i <= n;i++){
if(bestx[i]==1)
printf("%d ",i);
}
printf("\n装在第二艘船上的集装箱为:");
for( i = 1;i <= n;i++){
if(0==bestx[i])
printf("%d ",i);
}
}
}
void main(){
int C1,C2;//两艘船各自的载重量
printf("输入两艘船各自的载重量:");
scanf("%d %d",&C1,&C2);
int num;//集装箱数目
printf("\n输入集装箱数量:");
scanf("%d",&num);
int *x = (int*)malloc( (num+1) * sizeof(int) );//集装箱是否放入的记录
int *w = (int*)malloc( (num+1) * sizeof(int) );//每个集装箱的重量
printf("依次输入每个集装箱的重量:\n");
for(int i = 1;i <= num;i++)
scanf("%d",w+i);
MaxLoding(w,C1,num,x);
solveLoading(C2,x,num,w);
delete []x;
delete []w;
}
#include<iostream>
using namespace std;
typedef int* pointer;
template<class Type>
class Loding{
friend Type MaxLoding(Type [],Type,int,int[]);
private:
void Backtrack(int i);
int n,//集装箱数
*x,//当前解
*bestx;//当前最优解
Type *w,//集装箱重量数组
c,//第一艘船的载重量
cw,//当前载重量
bestw,//当前最优载重量
r;//剩余集装箱重量
};
template<class Type>
void Loding<Type>::Backtrack(int i)
{//搜索第i层节点
if(i > n){//到达叶节点
if(cw > bestw){
for(int j = 1;j <= n;j++)
bestx[j] = x[j];
bestw = cw;
}
return;
}
//搜索子树
r -= w[i];
if(cw + w[i] <= c){//搜索左子树
x[i] = 1;
cw += w[i];
Backtrack(i + 1);
cw -= w[i];
}
if(cw + r > bestw){//搜索右子树
x[i] = 0;
Backtrack(i+1);
}
r += w[i];
}
template<class Type>
Type MaxLoding(Type w[],Type c,int n,int bestx[]){
//返回最优载重量
Loding<Type> X;
//初始化X
X.x = (int*)malloc( (n+1) * sizeof(int) );
X.w = w;
X.c = c;
X.n = n;
X.bestx = bestx;
X.bestw = 0;
X.cw = 0;
//初始化r
X.r = 0;
for(int i = 1;i <= n;i++)
X.r += w[i];
X.Backtrack(1);
delete []X.x;
return X.bestw;
}
template<class Type>
void solveLoading(Type C2,int bestx[],int n,Type w[]){
int Weight = 0;//集装箱的总重量
int weightC1 = 0;//第一艘船上的集装箱的重量和
for(int i = 1;i <= n;i++){
Weight += w[i];
if(bestx[i]==1)
weightC1 += w[i];
}
if((Weight - weightC1) > C2)
printf("该装载问题无解!\n");
else
{
printf("装在第一艘船上的集装箱为:");
for(int i = 1;i <= n;i++){
if(bestx[i]==1)
printf("%d ",i);
}
printf("\n装在第二艘船上的集装箱为:");
for( i = 1;i <= n;i++){
if(0==bestx[i])
printf("%d ",i);
}
}
}
void main(){
int C1,C2;//两艘船各自的载重量
printf("输入两艘船各自的载重量:");
scanf("%d %d",&C1,&C2);
int num;//集装箱数目
printf("\n输入集装箱数量:");
scanf("%d",&num);
int *x = (int*)malloc( (num+1) * sizeof(int) );//集装箱是否放入的记录
int *w = (int*)malloc( (num+1) * sizeof(int) );//每个集装箱的重量
printf("依次输入每个集装箱的重量:\n");
for(int i = 1;i <= num;i++)
scanf("%d",w+i);
MaxLoding(w,C1,num,x);
solveLoading(C2,x,num,w);
delete []x;
delete []w;
}
相关文章推荐
- 【回溯法解决最优装载问题】
- 装载问题 回溯法解决
- 回溯法解决装载问题
- 回溯法解决装载问题
- 回溯法解决八皇后问题
- 回溯法解决N皇后问题(java实现)
- PHP回溯法解决0-1背包问题实例分析
- 用回溯法解决0-1背包问题
- 回溯法解决n皇后问题
- 回溯法解决01背包问题
- 用试探回溯法解决N皇后问题
- 第一次上传代码 处女秀-回溯法解决八皇后问题
- (回溯法)解决一系列组合问题
- 回溯法(backtracking)解决平衡集合问题
- Python基于回溯法子集树模板解决找零问题示例
- 回溯法解决八人过河问题
- Macromedia Dreamweaver 8 以下翻译器没有被装载问题解决办法[转]
- 用回溯法解决图着色问题
- 用深度优先搜索(DFS)(回溯法)解决N皇后问题(Leetcode 51)
- 回溯法解决方格填数问题java实现