@@ -46,10 +46,12 @@ def visit(city: int, mask: int) -> int:
46
46
if mask == VISITED_ALL :
47
47
return distances [city ][0 ] # Return to the starting city
48
48
49
- min_cost = float (' inf' )
49
+ min_cost = float (" inf" )
50
50
for next_city in range (n ):
51
51
if not mask & (1 << next_city ): # If the next_city is not visited
52
- new_cost = distances [city ][next_city ] + visit (next_city , mask | (1 << next_city ))
52
+ new_cost = distances [city ][next_city ] + visit (
53
+ next_city , mask | (1 << next_city )
54
+ )
53
55
min_cost = min (min_cost , new_cost )
54
56
return min_cost
55
57
@@ -60,5 +62,9 @@ def visit(city: int, mask: int) -> int:
60
62
import doctest
61
63
62
64
doctest .testmod ()
63
- print (f"{ tsp ([[0 , 10 , 15 , 20 ], [10 , 0 , 35 , 25 ], [15 , 35 , 0 , 30 ], [20 , 25 , 30 , 0 ]]) = } " )
64
- print (f"{ tsp ([[0 , 29 , 20 , 21 ], [29 , 0 , 15 , 17 ], [20 , 15 , 0 , 28 ], [21 , 17 , 28 , 0 ]]) = } " )
65
+ print (
66
+ f"{ tsp ([[0 , 10 , 15 , 20 ], [10 , 0 , 35 , 25 ], [15 , 35 , 0 , 30 ], [20 , 25 , 30 , 0 ]]) = } "
67
+ )
68
+ print (
69
+ f"{ tsp ([[0 , 29 , 20 , 21 ], [29 , 0 , 15 , 17 ], [20 , 15 , 0 , 28 ], [21 , 17 , 28 , 0 ]]) = } "
70
+ )
0 commit comments