#include #include #include #include #include /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; int ** READ(int N) { int num; int ** M = new int * [N]; for(int i = 0; i < N; i++) { M[i] = new int[N]; for(int j = 0; j < N; j++) { cin >> num; if(num < 0) M[i][j] = 1000000000; else M[i][j] = num; } } return M; } void PRINT(int ** M, int N) { for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) cout << setw(4) << M[i][j]; cout << endl; } } int main(int argc, char** argv) { int N, S, F; int ** matrix; vector a, b, c; cin >> N >> S >> F; S--; F--; matrix = READ(N); a.assign(N, 0); b.assign(matrix[S], matrix[S] + N); c.assign(N, S); a[S] = 1; /* PRINT(matrix, N); copy(a.begin(), a.end(), ostream_iterator(cout, " ")); cout << endl; copy(b.begin(), b.end(), ostream_iterator(cout, " ")); cout << endl; copy(c.begin(), c.end(), ostream_iterator(cout, " ")); cout << endl; */ int index = N; int min = INT_MAX; for(int i = 0; i < N; i++) { if(a[i] == 0 && b[i] < min) { index = i; min = b[i]; } } //for(int l = 0; index < N && l < 2; l++) while(index < N) { a[index] = 1; for(int k = 0; k < N; k++) { if(b[index] + matrix[index][k] < b[k]) { b[k] = b[index] + matrix[index][k]; c[k] = index; } } min = INT_MAX; index = N; for(int i = 0; i < N; i++) { if(a[i] == 0 && b[i] < min) { index = i; min = b[i]; } } /* copy(a.begin(), a.end(), ostream_iterator(cout, " ")); cout << endl; copy(b.begin(), b.end(), ostream_iterator(cout, " ")); cout << endl; copy(c.begin(), c.end(), ostream_iterator(cout, " ")); cout << endl; */ } if(b[F] == 1000000000) cout << -1; else cout << b[F]; return 0; }