Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int long long
- #define F first
- #define S second
- #define pb push_back
- using namespace std;
- int32_t main ()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- int n, s, f;
- cin >> n >> s >> f;
- s--;
- f--;
- vector < pair < int, int >> g[n];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- int x;
- cin >> x;
- if (x != -1) g[i].pb({j, x});
- }
- }
- int ans[n];
- for (int i = 0; i < n; i++)
- {
- ans[i] = 1e9;
- }
- priority_queue < pair < int, int>> q;
- q.push({0,s});
- while (!q.empty())
- {
- int v = q.top().S, r = q.top().F;
- r = -r;
- q.pop();
- if (ans[v] < r) continue;
- ans[v]=r;
- for (auto to:g[v])
- {
- if (ans[to.F] > r + to.S)
- {
- ans[to.F] = r + to.S;
- q.push({-(r + to.S), to.F});
- }
- }
- }
- if (ans[f] == 1e9) cout << -1;
- else cout << ans[f];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement