Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <vector>
- #include <string>
- #include <math.h>
- #include <climits>
- #define ll long long
- using namespace std;
- //freopen("a.in", "r", stdin);
- //freopen("a.out", "w", stdout);
- int a[2001][2001];
- ll used[2001];
- int n, s, t, x;
- ll xtofs(int v){
- if(v == s)
- return 0;
- ll m = LLONG_MAX;
- ll h;
- for(int i = 0; i < n; i++){
- if(a[v][i] > 0){
- if(used[i] == LLONG_MAX){
- h = xtofs(i);
- }else{
- h = used[i];
- }
- if(h != LLONG_MAX)
- m = min(m, h + a[v][i]);
- }
- }
- used[v] = m;
- return m;
- }
- int main()
- {
- ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- cin >> n >> s >> t;
- fill(used, used + 2001, LLONG_MAX);
- s--;
- t--;
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n; j++){
- cin >> x;
- a[j][i] = x;
- }
- }
- ll q = xtofs(t);
- if(q == LLONG_MAX)
- cout << -1;
- else
- cout << q;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement