本程序为算法导论中动态规划章节(15.1)的装配线调度问题的C++实现代码,具体问题就不详细说明了,代码如下:
#include<iostream> #include<string> const int max_len = 6; int a[max_len][2] = {{7,8},{9,5},{3,6},{4,4},{8,5},{4,7}};/*装配站a,i,j上的耗费时间*/ int t[max_len-1][2] = {{2,2},{3,1},{1,2},{3,2},{4,1}}; /*转配线转移的耗费时间*/ int e[2] = {2,4}; int x[2] = {3,2}; int f[max_len][2] = {{0,0}};/*到转配占si,j的最快的可能时间*/ int fs ;/*通过工厂的最快时间*/ int ls; int l[max_len][2] = {{0,0}}; void FastWay(); void Print(); int main() { FastWay(); Print(); return 0; } void Print() { int i = ls; std::cout<<"Lines "<<i<<", Station"<<max_len<<std::endl; for(int j=max_len-1;j>=1;j--) { i = l[j][i-1]; std::cout<<"Lines "<<i<<", Station"<<j<<std::endl; } } void FastWay() { int c1 = a[0][0] + e[0]; int c2 = a[0][1] + e[1]; f[0][0] = c1; f[0][1] = c2; for(int i=1;i<max_len;i++) { c1 = f[i-1][0] + a[i][0]; c2 = f[i-1][1] + t[i-1][1]+a[i][0]; if(c1 <= c2) { f[i][0] = c1; l[i][0] = 1; } else { f[i][0] = c2; l[i][0] = 2; } c1 = f[i-1][1] + a[i][1]; c2 = f[i-1][0] + t[i-1][0]+a[i][1]; if(c1 <= c2) { f[i][1] = c1; l[i][1] = 2; } else { f[i][1]= c2; l[i][1] = 1; } } c1 = f[max_len-1][0] + x[0]; c2 = f[max_len-1][1] + x[1]; if(c1 <= c2) { fs = c1; ls = 1; } else { fs = c2; ls = 2; } }