INF float inf int input for in range row list map int input split appe

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
INF = float('inf')
N = int(input())
W = []
for i in range(N):
row = list(map(int, input().split()))
W.append(row)
for i in range(N):
for j in range(N):
if W[i][j] < 0: W[i][j] = INF
active = [True]*N
R = W[0][:]
P = [0]*N
active[0] = False
P[0] = -1
for i in range(N-1):
minDist = INF
for j in range(N):
if active[j] and R[j] < minDist:
minDist = R[j]
kMin = j
active[kMin] = False
for j in range(N):
if R[kMin] + W[kMin][j] < R[j]:
R[j] = R[kMin] + W[kMin][j]
P[j] = kMin
print(N)
i = N-1
while i >= 0:
print ( i+1," ", end="" )
i = P[i]