@@ -38,23 +38,38 @@ def shortest_path(w, S, C):
38
38
#print("C: ", C)
39
39
#print("S, C[S]: ", S, C[S])
40
40
41
- path = []
42
- path .append (1 )
43
- path_length = INF
44
-
41
+ shortest_path_length = INF
42
+ for i in C [S ]:
43
+ if C [S ][i ] + w [i ][0 ] >= shortest_path_length :
44
+ continue
45
+ shortest_path_length = C [S ][i ] + w [i ][0 ]
46
+
47
+ if shortest_path_length == INF :
48
+ return - 1 , []
49
+
50
+ shortest_path = []
45
51
while S > 0 :
46
- prev_v = 1
52
+ prev_v = 0
53
+ curr_v = - 1
54
+ path_length = INF
47
55
for i in C [S ]:
48
- if C [S ][i ] + w [i ][1 ] >= path_length :
56
+ if C [S ][i ] + w [i ][prev_v ] >= path_length :
49
57
continue
58
+
59
+ path_length = C [S ][i ] + w [i ][prev_v ]
60
+ curr_v = i
50
61
51
- path_length = C [S ][i ] + w [i ][v ]
52
- v = - 1
53
-
54
-
55
-
56
-
57
- return - 1 if path_length == INF else path_length , path
62
+ if path_length == INF :
63
+ break
64
+
65
+ #print("C[S], curr_v, path_length: ", C[S], curr_v, path_length)
66
+ shortest_path .append (curr_v + 1 )
67
+ S = S ^ (1 << curr_v )
68
+ prev_v = curr_v
69
+ #print("S', path: ", S, shortest_path)
70
+
71
+ shortest_path .append (1 )
72
+ return shortest_path_length , shortest_path [::- 1 ]
58
73
59
74
def optimal_path (w ):
60
75
@@ -105,16 +120,16 @@ def optimal_path(w):
105
120
C_S [v_i ] = min (C_S [v_i ], C_S_minus_i [v_j ] + w [v_j ][v_i ])
106
121
j *= 2
107
122
#print(C)
108
- print (C [S ])
109
- path_length = INF
110
- for i in C [S ]:
111
- if C [S ][i ] + w [i ][1 ] >= path_length :
112
- continue
123
+ # print(C[S])
124
+ # path_length = INF
125
+ # for i in C[S]:
126
+ # if C[S][i] + w[i][1] >= path_length:
127
+ # continue
113
128
114
- path_length = C [S ][i ] + w [i ][0 ]
129
+ # path_length = C[S][i] + w[i][0]
115
130
116
- print (path_length )
117
- # return shortest_path(w, S, C)
131
+ # print(path_length)
132
+ return shortest_path (w , S , C )
118
133
119
134
def optimal_path_naive (graph ):
120
135
# This solution tries all the possible sequences of stops.
@@ -163,5 +178,5 @@ def read_data():
163
178
164
179
165
180
if __name__ == '__main__' :
166
- optimal_path (read_data ())
181
+ print_answer ( * optimal_path (read_data () ))
167
182
#print_answer(*optimal_path_naive(read_data()))
0 commit comments