Advertisement
nq1s788

Форд-Беллман

Apr 25th, 2024
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. inf = float('inf')
  2. n, m = map(int, input().split())
  3. mx = []
  4. for i in range(m):
  5.     x, y, w = map(int, input().split())
  6.     mx.append((x, y, w))
  7. start = int(input())
  8. w = [inf] * n #w[x] -- кратчайшее расстояние от start до x
  9. w[start] = 0
  10. for _ in range(n):
  11.     for vertex in mx:
  12.         if w[vertex[0]] > w[vertex[1]] + vertex[2]:
  13.             w[vertex[0]] = w[vertex[1]] + vertex[2]
  14.         if w[vertex[1]] > w[vertex[0]] + vertex[2]:
  15.             w[vertex[1]] = w[vertex[0]] + vertex[2]
  16.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement