Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<set>
- using namespace std;
- const int inf = (1 << 31) - 1;
- int main ()
- {
- int n,start,finish;
- cin >> n >> start >> finish;
- start--;finish--;
- vector <vector<pair<int,int>>> g(n);
- for ( int i = 0; i < n; i++)
- {
- for ( int j = 0; j < n; j++)
- {
- int w;
- cin >> w;
- if (w != -1 && i != j)
- {
- g[i].push_back({j,w});
- }
- }
- }
- vector <int> r(n,inf);
- r[start] = 0;
- set<pair<int,int>>s;
- s.insert({0,start});
- while (!s.empty())
- {
- int rmin = s.begin()->first;
- int cur = s.begin()->second;
- s.erase(s.begin());
- for (pair<int, int> to : g[cur])
- {
- if (r[cur] + to.second < r[to.first])
- {
- r[to.first] = r[cur] + to.second;
- s.insert({r[to.first], to.first});
- }
- }
- }
- if (r[finish] == inf)
- cout << -1;
- else
- cout << r[finish];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement