Advertisement
Egor_1425

Untitled

Apr 15th, 2024 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. INF = 1000000
  2.  
  3. def green(dist, color):
  4.     green_ = -1
  5.     min_ = INF
  6.     for i, t in enumerate(dist):
  7.         if t < min_ and i not in color:
  8.             min_ = t
  9.             green_ = i
  10.     return green_
  11.  
  12. n = int(input())
  13. d, k = map(int, input().split())
  14. r = int(input())
  15. graph = [[(INF, INF, -1)] for x in range(n)]
  16. for x in range(r):
  17.     s, t1, f, t2 = map(int, input().split())
  18.     graph[s-1].append((t1, t2, f-1))
  19.  
  20. v = d - 1
  21. dist = [INF] * n
  22. dist[v] = 0
  23. color = {v}
  24.  
  25. while v != -1:
  26.     for t1, t2, f in graph[v]:
  27.         if t1 != INF and t2 != INF and f != -1 and f not in color and dist[v] <= t1 and t2 < dist[f]:
  28.             dist[f] = t2
  29.     v = green(dist, color)
  30.     color.add(v)
  31.  
  32. if dist[k-1] == INF:
  33.     print(-1)
  34. else:
  35.     print(dist[k-1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement