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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
#include <iostream> #define FOR(i,n) for(int i=1;i<=n;i++) double gr[22][22][22]; int nxt[22][22][22]; int backtrck[22]; int n; using namespace std; int path(int step,int i,int j) { backtrck[step] = j; step-=1; while(step) { backtrck[step]=nxt[step][i][j]; step-=1; j= nxt[step][i][j]; } backtrck[step]=nxt[step+1][i][j]; return 0; } void init() { FOR(step,n) FOR(i,n) FOR(j,n) { nxt[1][i][j]=i; gr[step][i][j]=0; } } int main() { while(cin>>n) { init(); FOR(i,n) { FOR(j,n) { if(i==j) { gr[1][i][j]=1; continue; } else cin>>gr[1][i][j]; } } for(int step=2;step<=n;step++) FOR(k,n) { FOR(i,n) { FOR(j,n) { double tmp = gr[step-1][i][k]*gr[1][k][j]; if(tmp>gr[step][i][j]) { gr[step][i][j] = tmp; nxt[step][i][j] = k; } } } } int f=0; int targetNo; for(int step=2;step<=n;step++) { for(int i=1;i<=n;i++) { if(gr[step][i][i]>1.01) { f=step; targetNo=i; break; } } if(f) break; } //cout<<"f = "<<f<<"\n"; if(!f) { cout<<"no arbitrage sequence exists\n"; continue; } else { int index=0; int currentNo = targetNo; backtrck[index++] = targetNo; for(int s=f; s>=1; s--) { //path from targetNo to currentNo currentNo = nxt[s][targetNo][currentNo]; backtrck[index++] = currentNo; } for(int i=index-1; i>0; i--) cout<< backtrck[i]<<" "; cout<<backtrck[0]<<"\n"; } //for(int i=0;i<=f;i++) //cout<<backtrck[i]<<(i!=f?" ":"\n"); } return 0; } |