-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arbitrage.cpp
76 lines (66 loc) · 1.82 KB
/
Arbitrage.cpp
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
75
76
/** https://www.spoj.com/problems/ARBITRAG/
* #floyd-warshall #shortest-path
*/
#include<iostream>
#include<vector>
#include<map>
using namespace std;
const int maxElements = 30;
bool FloydWarShall(vector< vector<double> >& graph) {
int i, j, k;
int V = graph.size();
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (graph[i][j] < graph[i][k] * graph[k][j]) {
graph[i][j] = graph[i][k] * graph[k][j];
}
}
}
}
// no need to check negative-cycles
return true;
}
int main() {
int n;
int testcase = 0;
while (cin >> n) {
if (n == 0) break;
testcase++;
map<string, int> currencyMap;
string tmp;
for (int i=0; i < n; i++) {
cin >> tmp;
currencyMap[tmp] = i;
//currencyMap.insert(pair<string, int>(tmp, i));
// note: if use [] of map/ unorder_map, if not exist -> auto create.
}
int m;
cin >> m;
string org, exch;
double rate;
vector< vector<double> > graph(n, vector<double>(n, 0.0));
for (int i=0; i < n; i++) {
graph[i][i] = 1.0;
}
int i, j;
for (int k=0; k < m; k++) {
cin >> org >> rate >> exch;
i = currencyMap[org];
j = currencyMap[exch];
if (rate > graph[i][j]) {
graph[i][j] = rate;
}
}
FloydWarShall(graph);
bool possibleFlg = false;
for (int i=0; i < n; i++) {
if (graph[i][i] > 1.0) {
possibleFlg = true;
break;
}
}
cout << "Case " << testcase << ": " << (possibleFlg ? "Yes" : "No") << endl;
}
return 0;
}