N: int = 4 MAX_WEIGHT: int = 4000 NO_PATH: int = -1 City_Graph = [[int('0')] * (N+1) for _ in range(N+1)] # 初始化dp x = [int('0') * (N+1) for _ in range(N+1)] # 保存第i步便利的城市 isIn = [int('0') * (N+1) for _ in range(N+1)] # 保存城市i是否已加入路径 bestx = [int('0') * (N+1) for _ in range(N+1)] # 最优路径 def Travel_Backtrack(t: int): global bestw, cw if t > N: # 走完了,输出结果 for i in range(1, N+1): # 输出当前路径 print(x[i], end=" ") print() if cw < bestw: for i in range(1, N + 1): bestx[i] = x[i] bestw = cw return else: for j in range(1, N+1): if City_Graph[x[t - 1]][j] != NO_PATH and (not isIn[j]): # 能到而且没有加入到路径中 isIn[j] = 1 x[t] = j cw = cw + City_Graph[x[t - 1]][j] Tr
分支限界法---旅行售货员问题
最新推荐文章于 2023-01-07 23:00:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)