动态规划——装配线调度

23 篇文章 0 订阅
/**
 * @brief Algorithm 15.1 
 * @author An
 * @data  2013.8.15                                                                  
**/

#include <iostream>
#define min( x, y ) ( x <= y ? x : y )

using namespace std;

static int f_result = 0;
static int **l = new int *[2];
static int l_result;

// void FastestWay( int a[2][], int t[2][], int e[2], int x[2], int n )  // test later
void FastestWay( int **a, int **t, int *e, int *x, int n )
{
	int **f = new int *[2];
	for ( int i = 0; i != 2; ++i )
	{
		f[i] = new int[n];
		l[i] = new int[n - 1];
	}

	f[0][0] = e[0] + a[0][0];
	f[1][0] = e[1] + a[1][0];
	for ( int i = 1; i != n; ++i )
	{
// 		f[0][i] = min( f[0][i - 1] + a[0][i], f[1][i - 1] + t[1][i - 1] + a[0][i] );
// 		f[1][i] = min( f[1][i - 1] + a[1][i], f[0][i - 1] + t[0][i - 1] + a[1][i] );

		if ( f[0][i - 1] + a[0][i] <= f[1][i - 1] + t[1][i - 1] + a[0][i] )
		{
			f[0][i] = f[0][i - 1] + a[0][i];
			l[0][i - 1] = 0;
		}
		else
		{
			f[0][i] = f[1][i - 1] + t[1][i - 1] + a[0][i];
			l[0][i - 1] = 1;
		}
		if ( f[1][i - 1] + a[1][i] <= f[0][i - 1] + t[0][i - 1] + a[1][i] )
		{
			f[1][i] = f[1][i - 1] + a[1][i];
			l[1][i - 1] = 1;
		}
		else
		{
			f[1][i] = f[0][i - 1] + t[0][i - 1] + a[1][i];
			l[1][i - 1] = 0;
		}
	}
//	f_result = min( f[0][n - 1] + x[0], f[1][n - 1] + x[1] );
	if ( f[0][n - 1] + x[0] <= f[1][n - 1] + x[1] )
	{
		f_result = f[0][n - 1] + x[0];
		l_result = 0;
	}
	else
	{
		f_result = f[1][n - 1] + x[1];
		l_result = 1;
	}
}

void printStations( int **l, int l_result, int n )
{
	int tmp = l_result;

	cout << "Station " << n << ": " << tmp << endl;
	for ( int i = n - 1; i != 0; --i )
	{
		tmp = l[tmp][i - 1];
		cout << "Station " << i << ": " << tmp << endl;
		
	}
}

int main()
{
	int n = 6;
	int e[] = { 2, 4 };
	int x[] = { 3, 2 };
	int aa[2][6] = { { 7, 9, 3, 4, 8, 4 }, { 8, 5, 6, 4, 5, 7 } };
	int tt[2][5] = { { 2, 3, 1, 3, 4 }, { 2, 1, 2, 2, 1 } };

	int **a = new int *[2];
	int **t = new int *[2];
	for ( int i = 0; i != 2; ++i )
	{
		a[i] = new int[n];
		t[i] = new int[n - 1];
	}
	for ( int i = 0; i != 2; ++i )
	{
		for ( int j = 0; j != n - 1; ++j )
		{
			a[i][j] = aa[i][j];
			t[i][j] = tt[i][j];
		}
		a[i][n - 1] = aa[i][n - 1];
	}
	
	FastestWay( a, t, e, x, n );
	cout << f_result << endl;
	printStations( l, l_result, n );

	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值