def fastest_way(a:list, t:list, e:list, x:list, n:int) -> list:
'''
解决最优排程问题
:param a: 两条线上各自的每个站点的各自处理时间
:param t: 两条线上各自的每个前 n - 1 个站点的切换时间
:param e: 两条线的各自的 entry 时间
:param x: 两条线的各自的 exit 时间
:param n: 每条线的工作站的数目
:return: 最优时间, 路径和最后一个走过的工作站点
'''
a1, a2 = a
t1, t2 = t
e1, e2 = e
x1, x2 = x
f1 = [0 for i in range(n)]
f2 = [0 for i in range(n)]
route = [[0 for i in range(n)], [0 for i in range(n)]]
time = float('inf')
final_line = 0
f1[0] = e1 + a1[0]
route[0][0] = 1
f2[0] = e2 + a2[0]
route[1][0] = 2
for i in range(1, n):
if f1[i - 1] + a1[i] < f2[i - 1] + t2[i - 1] + a1[i]:
f1[i] = f1[i - 1] + a1[i]
route[0][i] = 1
else:
f1[i] = f2[i - 1] + t2[i - 1] + a1[i]
route[0][i] = 2
if f2[i - 1] + a2[i] < f1[i - 1] + t1[i - 1] + a2[i]:
f2[i] = f2[i - 1] + a2[i]
route[1][i] = 2
else:
f2[i] = f1[i - 1] + t1[i - 1] + a2[i]
route[1][i] = 1
if (f1[n - 1] + x1 < f2[n - 1] + x2):
time = f1[n - 1] + x1
final_line = 1
else:
time = f2[n - 1] + x2
final_line = 2
return [time, route, final_line]
def print_route(i:int, j:int, route:list, n:int):
'''
递归打印装配的线路图
:param i: 装配线, 0 represents the first assembly line, 1 represents the second line
:param j: 当前节点
:param route: 存储两条线路的节点的父节点的 list
:param n: 装配线的节点数
:return: None
'''
if j == 1:
print('[Line: ' + str(i) + ' Station: ' + str(j) + ']', end=' ')
else:
print_route(route[i - 1][j - 1], j - 1, route, n)
print('[Line: ' + str(i) + ' Station: ' + str(j) + ']', end=' ')
if j != n:
print('=>', end=' ')
else:
print()
if __name__ == '__main__':
n = 5
a1 = [7, 9, 3, 4, 8]
a2 = [8, 5, 6, 4, 5]
t1 = [2, 3, 1, 3]
t2 = [2, 1, 2, 2]
e1 = 2
e2 = 4
x1 = 3
x2 = 6
a = [a1, a2]
t = [t1, t2]
e = [e1, e2]
x = [x1, x2]
res = fastest_way(a, t, e, x, n)
time, route, final_line = res
print('The shortest time:', time)
print('The best assembly line is: ', end='')
print_route(final_line, 5, route, n)