动态规划1——之工厂生产线问题

376人阅读 评论(0)

#include <iostream>

#define LINELENGTH 6

int line_1[] = {7, 9, 3, 4, 8, 4} ;

int line_2[] = {8, 5, 6, 4, 5, 7} ;

int time_1[] = {2, 2, 1, 2, 2, 1} ;

int time_2[] = {4, 2, 3, 1, 3, 4} ;

int best_line_1[6] = {0} ;

int best_line_2[6] = {0} ;

int short_time_line_1[6] = {0} ;

int short_time_line_2[6] = {0} ;

int time_line1_to_end = 3 ;

int time_line2_to_end = 2 ;

int lastStep ;

int lastTime;

void PrintLine(int stationNumber,int line )

{

if ( stationNumber > 0 ) {

if ( line == 2) {

PrintLine(stationNumber -1 , best_line_2[stationNumber - 1]) ;

std::cout<<"Station"<<stationNumber<<" Line 2"<<std::endl;

else {

PrintLine(stationNumber -1 , best_line_1[stationNumber - 1]) ;

std::cout<<"Station"<<stationNumber<<" Line 1"<<std::endl;

}

}

}

void FindBest()

{

best_line_1[0] = 1 ;

best_line_2[0] = 2;

short_time_line_1[0] = time_1[0] + line_1[0];

short_time_line_2[0] = time_2[0] + line_2[0];

for(int i = 1; i < 6 ; i++) {

if (short_time_line_1[i-1] > short_time_line_2[i-1] + time_1[i]) {

short_time_line_1[i] = short_time_line_2[i-1] + time_1[i] + line_1[i];

best_line_1[i] = 2;

}

else {

short_time_line_1[i] = short_time_line_1[i-1] +line_1[i] ;

best_line_1[i] = 1;

}

if (short_time_line_2[i-1] > short_time_line_1[i-1] + time_2[i]) {

short_time_line_2[i] = short_time_line_1[i-1] + time_2[i] + line_2[i] ;

best_line_2[i] = 1;

else {

short_time_line_2[i] = short_time_line_2[i-1] +line_2[i] ;

best_line_2[i] = 2;

}

} //end of for

if ( short_time_line_1[LINELENGTH -1] + time_line1_to_end > short_time_line_2[LINELENGTH -1] + time_line2_to_end ) {

lastTime = short_time_line_2[LINELENGTH -1] + time_line2_to_end ;

lastStep = 2 ;

else

{

lastTime = short_time_line_1[LINELENGTH -1] + time_line1_to_end;

lastStep = 1 ;

}

std::cout<<"Total time is "<<lastTime<<std::endl;

if ( lastStep > 1){

PrintLine(LINELENGTH, 2) ;

//std::cout<<"Station"<<LINELENGTH<<"Line 2"<<std::endl;

else {

PrintLine(LINELENGTH, 1) ;

//std::cout<<"Station"<<LINELENGTH<<"Line 1"<<std::endl;

}

}

int main()

{

FindBest();

getchar();

return 0;

}

0
0

【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：10658次
• 积分：328
• 等级：
• 排名：千里之外
• 原创：24篇
• 转载：0篇
• 译文：0篇
• 评论：2条
评论排行
最新评论