您的位置:首页 > 其它

第十五章动态规划之“装配线调度”

2012-05-03 13:41 309 查看
动态规划研究的问题与分治法区别:动态规划的子问题不是相互独立的,而是有交集,即子问题相互重叠,为了避免重复计算重叠的子问题,所以选择从底向上的迭代,以后用到直接查表。从而将装配线调度的复杂度从O(2^n)降到了O(n)。

代码如下:

#include <iostream>
using namespace std;

void FastestWay(int (*a)[7],int (*t)[7],int *e,int *x,int n,int (*f)[7],int (*l)[7],int &ff,int &ll)
{
f[0][1]=e[0]+a[0][1];
f[1][1]=e[1]+a[1][1];

for(int j=2;j<=n;j++)
{
if(f[0][j-1]+a[0][j]<f[1][j-1]+t[1][j-1]+a[0][j])
{
f[0][j]=f[0][j-1]+a[0][j];
l[0][j]=1;
}
else
{
f[0][j]=f[1][j-1]+t[1][j-1]+a[0][j];
l[0][j]=2;
}

if(f[1][j-1]+a[1][j]<f[0][j-1]+t[0][j-1]+a[1][j])
{
f[1][j]=f[1][j-1]+a[1][j];
l[1][j]=2;
}
else
{
f[1][j]=f[0][j-1]+t[0][j-1]+a[1][j];
l[1][j]=1;
}
}

if(f[0]
+x[0]<f[1]
+x[1])
{
ff=f[0]
+x[0];
ll=1;
}
else
{
ff=f[1]
+x[1];
ll=2;
}
}

void PrintStations(int (*l)[7],int i,int j)
{
if(j==1)
{
cout<<"line "<<i<<" ,station "<<j<<endl;
}
else
{
int ti=i;
i=l[i-1][j];
int tj=j;
j--;
PrintStations(l,i,j);
cout<<"line "<<ti<<" ,station "<<tj<<endl;
}
}

int main()
{
int n=6;
int a[2][7]={{0,7,9,3,4,8,4},{0,8,5,6,4,5,7}};
int t[2][7]={{0,2,3,1,3,4},{0,2,1,2,2,1}};
int e[2]={2,4};
int x[2]={3,2};
int f[2][7],l[2][7];
int ff=0,ll=0;
FastestWay(a,t,e,x,n,f,l,ff,ll);
PrintStations(l,ll,6);
//cout<<ff<<" "<<ll;
system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: