您的位置:首页 > 其它

算法导论-装配线调度问题

2012-02-06 15:58 369 查看
动态规划

void Fastest_Way()
{
//there is only one way to get to the first station in each line
f[0][0]=e[0]+a[0][0];
f[1][0]=e[1]+a[1][0];

//two way to get to the i's station, we will choose the lower one.
for(int i=1;i<n;++i)
{
if(f[0][i-1]+a[0][i]<f[1][i-1]+t[1][i-1]+a[0][i])
{
f[0][i]=f[0][i-1]+a[0][i];
l[0][i]=0;
}
else
{
f[0][i]=f[1][i-1]+t[1][i-1]+a[0][i];
l[0][i]=1;
}

if(f[1][i-1]+a[1][i]<f[0][i-1]+t[0][i-1]+a[1][i])
{
f[1][i]=f[1][i-1]+a[1][i];
l[1][i]=1;
}
else
{
f[1][i]=f[0][i-1]+t[0][i-1]+a[1][i];
l[1][i]=0;
}
}
//when passing the last station, we should add the exit price, the lower will be the fastest
if(f[0][n-1]+x[0]<f[1][n-1]+x[1])
{
_f=f[0][n-1]+x[0];
_l=0;
}
else
{
_f=f[1][n-1]+x[1];
_l=1;
}
cout <<"The fastest time is "<<_f<<endl;

//get the full path backward, using a stack is a good way
stack<int> s;
s.push(_l);
for(int i=n-1;i>=1;--i)
{
s.push(l[_l][i]);
_l=l[_l][i];
}
cout <<"The fastest path is: ";
for(int i=0;i<n;++i)
{
cout <<"S"<<"["<<s.top()+1<<"]"<<"["<<i+1<<"]";
if(i!=n-1)
cout <<"->";
s.pop();
}
cout <<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: