-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path104 - Arbitrage.cpp
68 lines (68 loc) · 1.26 KB
/
104 - 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
/**
UVa 104 - Arbitrage
by Rico Tiongson
Submitted: May 3, 2013
Accepted: 0.112s C++
O(n^4) time
*/
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
int n,path[22][22][22],temp; //for floyd-warshall
double d[22][22][22],x;
bool b;
stack<int> s;
void floydwarshall2(){
for(int l=1;l<n;l++){
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
x = d[i][k][l-1]*d[k][j][0];
if(d[i][j][l] < x){
d[i][j][l] = x;
path[i][j][l] = k; //depth
}
}
}
}
}
//get prevs
b=false;
for(int l=1;l<n&&!b;l++){
for(int i=0;i<n;i++){
if(d[i][i][l]>1.01){
s.push(path[i][i][l]);
for(int j=l-1;j>-1;j--) s.push(path[i][s.top()][j]);
temp = s.top();
while(!s.empty()){
cout << s.top()+1 << ' ';
s.pop();
}
cout << temp+1;
b=true;
break;
}
}
}
if(!b) cout << "no arbitrage sequence exists";
cout << endl;
}
int main(){
while(cin >> n){
memset(d,0,sizeof(d));
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
cin >> d[i][j][0];
path[i][j][0]=i;
}
d[i][i][0]=1.;
path[i][i][0]=i;
for(int j=i+1;j<n;j++){
cin >> d[i][j][0];
path[i][j][0]=i;
}
}
floydwarshall2();
}
}