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

#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;

}

06-02 4072

11-05 2421

04-27 2895

08-18 1146

11-28 3157

01-01 2173

01-29 191

05-29 463

11-07 3525

12-15 1919