Advertisement
SetKaung

Currency Exchange

Sep 7th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. def bellman_ford(n, edges, start, initial_money):
  2.     dist = [-float('inf')] * n
  3.     dist[start] = initial_money
  4.    
  5.  
  6.     for _ in range(n - 1):
  7.         for u, v, rate, commission in edges:
  8.             if dist[u] > 0:
  9.                 new_amount = (dist[u] - commission) * rate
  10.                 if new_amount > dist[v]:
  11.                     dist[v] = new_amount
  12.    
  13.  
  14.     for u, v, rate, commission in edges:
  15.         if dist[u] > 0:
  16.             new_amount = (dist[u] - commission) * rate
  17.             if new_amount > dist[v]:
  18.                 return True  
  19.    
  20.     return False
  21.  
  22. n, m, s, v = map(float, input().split())
  23. edges = []
  24. n = int(n)
  25. m = int(m)
  26. s = int(s)
  27.  
  28. for _ in range(m):
  29.     a, b, rab, cab, rba, cba = map(float, input().split())
  30.     a, b = int(a) - 1, int(b) - 1
  31.     edges.append((a, b, rab, cab))
  32.     edges.append((b, a, rba, cba))
  33.  
  34. if bellman_ford(n, edges, s - 1, v):
  35.     print("YES")
  36. else:
  37.     print("NO")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement