-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arbitrage.py
74 lines (62 loc) · 1.32 KB
/
Arbitrage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from math import log, exp
INF = float("inf")
def FloydWarshall(dist, n):
for k in range(n):
for i in range(n):
for j in range(n):
new_w = dist[i][k] + dist[k][j]
if new_w < dist[i][j]:
dist[i][j] = new_w
case = 1
while True:
n = int(input())
if n == 0: break
currencies = {}
for i in range(n):
c = input()
currencies[c] = i
dist = [[INF] * n for _ in range(n)]
for i in range(n):
dist[i][i] = 0
# input()
m = int(input())
for _ in range(m):
line = input().split()
c1, r, c2 = line[0], -log(float(line[1])), line[2]
dist[currencies[c1]][currencies[c2]] = r
# print(dist)
FloydWarshall(dist, n)
# print(dist)
valid = True
for i in range(n):
if dist[i][i] >= 0:
valid = False
break
if valid:
print(f"Case {case}: Yes")
else:
print(f"Case {case}: No")
case+=1
input()
"""
3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar
3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar
0
"""