您的位置:首页 > 其它

算法导论 15章 动态规划 装配线调度算法

2013-04-19 16:53 375 查看
已知:





//算法导论 15章 动态规划
//装配线调度算法.cpp
#include <iostream>
using namespace std;

#define M 3
#define N 10
int f[M]
;//函数体外定义的数组自动初始化为零.
int l[M]
;
int minTime = 0;//通过工厂的最短时间.
int endLineNumber;//表示最后离开工厂时所在的装配线(取1或2).
int endStationNumber;
int line
;

//a[i][j]表示装配站S[i][j]的装配时间.(*a)
表示a是指向存放N个int型的数组的指针。
//t[i][j]表示装配站S[i][j-1]移动到下一装配站S[k][j]的时间,若k等于i则t[i][j]为0.
//e[i]表示从底盘进入装配线i所需的时间.
//n表示每条装配线中装配站的个数.
void fastestWay(int (*a)
,int (*t)
,int *e,int *x,const int n)
{
f[1][1] = e[1] + a[1][1];
f[2][1] = e[2] + a[2][1];
for (int j = 2; j <= n;++j)
{//通过以递增装配站编号j的顺序计算f[i][j]的值,可在O(n)时间内求出通过工厂的最快线路.
if (f[1][j-1] + a[1][j] <= f[2][j-1]+t[2][j-1]+a[1][j])
{//从装配站S[1][j-1]转到S[1][j].
f[1][j] = f[1][j-1] + a[1][j];
l[1][j] = 1;
}
else
{//从装配站S[2][j-1]转到S[1][j].
f[1][j] = f[2][j-1]+t[2][j-1]+a[1][j];
l[1][j] = 2;
}
if (f[2][j-1] + a[2][j] <= f[1][j-1]+t[1][j-1]+a[2][j])
{//从装配站S[2][j-1]转到S[2][j].
f[2][j] = f[2][j-1] + a[2][j];
l[2][j] = 2;
}
else
{//从装配站S[1][j-1]转到S[2][j].
f[2][j] = f[1][j-1]+t[1][j-1]+a[2][j];
l[2][j] = 1;
}
}

//判断出厂时所在的线路
if (f[1]
+ x[1] <= f[2]
+ x[2])
{
minTime = f[1]
+ x[1];
endLineNumber = 1;
}
else
{
minTime = f[2]
+ x[2];
endLineNumber = 2;
}
endStationNumber = n;
}

void printLine()
{
//结束时从线路endLineNumber出厂
cout << "line: " << endLineNumber << ", station: " << endStationNumber << endl;
int lineNum = endLineNumber;//lineNum表示当前所在装配线
for (;endStationNumber >= 2;--endStationNumber)
{
cout << "line: " << l[lineNum][endStationNumber] << ", station: " << endStationNumber - 1 << endl;
lineNum = l[lineNum][endStationNumber];
}
}

int main()
{
int a[3]
= {{0},{0,7,9,3,4,8,4},{0,8,5,6,4,5,7}};
int t[3]
= {{0},{0,2,3,1,3,4},{0,2,1,2,2,1}};
int e[3] = {0,2,4};
int x[3] = {0,3,2};
int n = 6;
fastestWay(a,t,e,x,n);
cout << "出厂时所在装配线为: " << endLineNumber << endl;
printLine();
cout << "最快出场时间为: " << minTime << endl;

return 0;
}

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: