Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def bellman_ford(n, edges, start, initial_money):
- dist = [-float('inf')] * n
- dist[start] = initial_money
- for _ in range(n - 1):
- for u, v, rate, commission in edges:
- if dist[u] > 0:
- new_amount = (dist[u] - commission) * rate
- if new_amount > dist[v]:
- dist[v] = new_amount
- for u, v, rate, commission in edges:
- if dist[u] > 0:
- new_amount = (dist[u] - commission) * rate
- if new_amount > dist[v]:
- return True
- return False
- n, m, s, v = map(float, input().split())
- edges = []
- n = int(n)
- m = int(m)
- s = int(s)
- for _ in range(m):
- a, b, rab, cab, rba, cba = map(float, input().split())
- a, b = int(a) - 1, int(b) - 1
- edges.append((a, b, rab, cab))
- edges.append((b, a, rba, cba))
- if bellman_ford(n, edges, s - 1, v):
- print("YES")
- else:
- print("NO")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement