1
- # python3
2
1
from itertools import permutations
3
2
4
3
INF = 10 ** 9
5
-
6
4
# Analysis:
7
5
# Input data structure: a weighted graph
8
6
# ... Bus depot, children homes and the school are represented with vertices
@@ -75,7 +73,7 @@ def optimal_path(w):
75
73
map_v_bit_position = dict ()
76
74
for i in range (n ):
77
75
map_v_bit_position [b ] = i
78
- b *= 2
76
+ b = b << 1
79
77
80
78
# C(S, i)
81
79
C = dict ()
@@ -87,8 +85,8 @@ def optimal_path(w):
87
85
C_S = C [s ]
88
86
89
87
i = 1
90
- while i * 2 <= s : # for all vertices i in s
91
- i *= 2
88
+ while ( i << 1 ) <= s : # for all vertices i in s
89
+ i = i << 1
92
90
v_i = map_v_bit_position [i ]
93
91
94
92
if i & s == 0 : # the vertice i isn't included in s
@@ -107,12 +105,12 @@ def optimal_path(w):
107
105
while j <= s_minus_i : #for all vertices j in s (j != i):
108
106
109
107
if s_minus_i & j == 0 : # the vertice j isn't included in s_minus_i
110
- j *= 2
108
+ j = j << 1
111
109
continue
112
110
113
111
v_j = map_v_bit_position [j ]
114
112
C_S [v_i ] = min (C_S [v_i ], C_S_minus_i [v_j ] + w [v_j ][v_i ])
115
- j *= 2
113
+ j = j << 1
116
114
117
115
return shortest_path (w , S , C )
118
116
@@ -160,8 +158,6 @@ def read_data():
160
158
161
159
return graph
162
160
163
-
164
-
165
161
if __name__ == '__main__' :
166
162
print_answer (* optimal_path (read_data ()))
167
163
#print_answer(*optimal_path_naive(read_data()))
0 commit comments