Write a Python program that solves the travelling salesperson problem.
Sample output:
Input: cost for every path:
Cost from 1-2: 10
Cost from 1-3: 15
Cost from 1-4: 20
Cost from 2-3: 35
Cost from 2-4: 25
Cost from 3-4: 30
Output:
Optimal Route: 12431
Total Path Cost: 80
route = list(str(input("Enter optimal route: ")))
cost = 0
for i in range(1,len(route)):
    if route[i-1] == "1" and route[i] == "2" or route[i-1] == "2" and route[i] == "1":
        cost += 10
    elif route[i-1] == "1" and route[i] == "3" or route[i-1] == "3" and route[i] == "1":
        cost += 15
    elif route[i-1] == "1" and route[i] == "4" or route[i-1] == "4" and route[i] == "1":
        cost += 20
    elif route[i-1] == "2" and route[i] == "3" or route[i-1] == "3" and route[i] == "2":
        cost += 35
    elif route[i - 1] == "2" and route[i] == "4" or route[i - 1] == "4" and route[i] == "2":
        cost += 25
    elif route[i - 1] == "3" and route[i] == "4" or route[i - 1] == "4" and route[i] == "3":
        cost += 30
print(cost)
Comments