算法导论 第15章 动态规划 装配线调度问题

#include<iostream>
using namespace std;
int fa,la;
int f[2][6]={0};
int L[2][5]={0};
void FASTEST_WAY(int a1[][6],int t1[][5],int e1[],int x1[],int n)//n为装配站的数目 0为第一条装配线 1为第二条装配线
{
	int j=0;
	f[0][0]=e1[0]+a1[0][0];
	f[1][0]=e1[1]+a1[1][0];
	for(j=1;j<n;j++)
	{
		if(f[0][j-1]+a1[0][j]<=f[1][j-1]+t1[1][j-1]+a1[0][j])
		{
			f[0][j]=f[0][j-1]+a1[0][j];
			L[1][j]=1;
		}
		else
		{
			f[0][j]=f[1][j-1]+t1[1][j-1]+a1[0][j];
			L[1][j]=2;
		}
		if(f[1][j-1]+a1[1][j]<=f[0][j-1]+t1[0][j-1]+a1[1][j])
		{
			f[1][j]=f[1][j-1]+a1[1][j];
			L[2][j]=2;
		}
		else
		{
			f[1][j]=f[0][j-1]+t1[0][j-1]+a1[1][j];
			L[2][j]=1;
		}
	}
	if(f[0][n-1]+x1[0]<=f[1][n-1]+x1[1])
	{
		fa=f[0][n-1]+x1[0];
		la=1;
	}
	else
	{
		fa=f[1][n-1]+x1[1];
		la=2;
	}
}
void PRINT_STATIONS(int la1,int n)
{
	int i=la1;
	cout<<"line"<<i<<","<<"station"<<n<<endl;
	for(int j=n-1;j>=1;j--)
	{
		i=L[i][j];
		cout<<"line"<<i<<","<<"station"<<j<<endl;
	}
}
int main()
{
	int n=0,i,j;
	int a[2][6]={{7,9,3,4,8,4},{8,5,6,4,5,7}};
	int t[2][5]={{2,3,1,3,4},{2,1,2,2,1}};
	int e[2]={2,4};
	int x[2]={3,2};
	cout<<"Enter n pleace: ";
	cin>>n;
	FASTEST_WAY(a,t,e,x,n);
	for(i=0;i<2;i++)
		for(j=0;j<n;j++)
		{
			if(j%6==0)
				cout<<endl;
			cout<<f[i][j]<<' ';	
		}
	cout<<endl;
	for(i=1;i<=2;i++)
		for(j=1;j<n;j++)
		{
			cout<<L[i][j]<<' ';
			if(j%5==0)
				cout<<endl;
		}
	cout<<endl<<fa<<endl;
	cout<<la<<endl;
	PRINT_STATIONS(la,n);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值