装载问题
1、算法设计:
回溯法,用子集树表示其解空间;
template
class Loading
{
public:
void Backtrack(int i);
int n,
*x,
*bestx;
Type *w,
c,
cw,
bestw,
r;
};
template
voidLoading ::Backtrack (int i);
template
Type MaxLoading(Type w[], Type c, int n, int bestx[]);
【装载问题】2、上界函数:
template
voidLoading ::Backtrack (int 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
Type MaxLoading(Type w[], Type c, int n, int bestx[])
{
LoadingX;
X.x=new int[n+1];
X.w=w;
X.c=c;
X.n=n;
X.bestx=bestx;
X.bestw=0;
X.cw=0;
X.r=0;
for (int i=1;
i<=n;
i++)
{
X.r+=w[i];
}
X.Backtrack(1);
delete []X.x;
return X.bestw;
}
推荐阅读
- parallels|parallels desktop 解决网络初始化失败问题
- 一个人的碎碎念
- 野营记-第五章|野营记-第五章 讨伐梦魇兽
- Shell-Bash变量与运算符
- 清明,是追思、是传承、是感恩。
- jhipster|jhipster 升级无效问题
- 牛人进化+|牛人进化+ 按自己的意愿过一生
- 七老修复好敏感、角质层薄、红血丝
- 华为旁!大社区、地铁新盘,佳兆业城市广场五期!
- “精神病患者”的角度问题