Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<set>
- #include <algorithm>
- 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);
- vector <int> p(n);
- 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;
- p[to.first] = cur;
- s.insert({ r[to.first], to.first });
- }
- }
- }
- if (r[finish] == inf)
- cout << -1;
- else
- {
- vector<int> path;
- for (int v = finish; v != start; v = p[v])
- path.push_back(v + 1);
- path.push_back(start + 1);
- reverse(path.begin(), path.end());
- for (int i = 0; i < path.size(); i++)
- cout << path[i] << ' ';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement