动态规划1——之工厂生产线问题
2011-01-19 00:18
357 查看
#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;
}
#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;
}
相关文章推荐
- 编程练习-动态规划(生产线问题)
- SDUT_取数字问题_动态规划
- DP 动态规划 Problem Q 1017 背包问题
- 01背包问题(动态规划DP)
- 最长公共子序列问题——动态规划
- 普及练习场 动态规划的背包问题 开心的金明
- LCS问题——动态规划
- hdu1176免费馅饼(数塔问题,动态规划)
- 动态规划 - 背包问题
- 每周算法练习——用动态规划求解最短路径问题
- uva 116 动态规划 多阶段决策问题 路径记录 lrj-P270
- 动态规划 钢条切割问题的朴素解法
- 动态规划三部曲之01背包问题的分析和实现(二)
- I - 数塔(动态规划)数塔问题
- 0017算法笔记——【动态规划】电路布线问题
- 最长递增、递减子序列(导弹拦截问题)动态规划练习
- 1040 -- 用动态规划求解矩阵乘法链问题
- LCS问题(最长公共子序列)-动态规划实现
- sdut.acm 2012级《程序设计基础Ⅱ)》_动态规划 最长公共子序列问题
- 买书问题——动态规划C++