задача 5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
n, s, f = [int(j) for j in input().split()]
b, c, d, e, visited = [], [100*100]*n, 10000, s-1, [False]*n
for i in range(n):
a = [int(j) for j in input().split()]
b.append(a)
c[s-1]=0
visited[s-1]=True
for i in range(n):
if b[s-1][i]>0:
c[i]=b[s-1][i]
for j in range(n):
for i in range(n):
if c[i]<d and c[i]>0 and i!=e and not visited[i]:
d=c[i]
e=i
for k in range(n):
if b[e][k]>0 and c[e]+b[e][k]<c[k]:
c[k]=c[e]+b[e][k]
visited[e]=True
d=10000
if c[f-1]<10000:
print(c[f-1])
else:
print(-1)