Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <stack>
- #include "optimization.h"
- using std::vector;
- using std::cin;
- using std::cout;
- using std::pair;
- using std::stack;
- int main() {
- int n, m, s, f;
- //cin >> n >> m >> s >> f;
- n = readInt();
- m = readInt();
- s = readInt();
- f = readInt();
- s--;
- f--;
- long long mnogo = 10000000000;
- vector<int> par(n, -1);
- vector<long long> dist(n, mnogo);
- vector<bool> used(n, false);
- vector<pair<int, int>> g[n];
- dist[s] = 0;
- int a, b, w;
- for (int i = 0; i < m; i++){
- //cin >> a >> b >> w;
- a = readInt();
- b = readInt();
- w = readInt();
- g[a - 1].push_back({b - 1, w});
- g[b - 1].push_back({a - 1, w});
- }
- for (int i = 0; i < n; i++){
- long long dmin = mnogo;
- int v = 0;
- for (int j = 0; j < n; j++) {
- if (!used[j])
- if (dist[j] < dmin) {
- v = j;
- dmin = dist[j];
- }
- }
- used[v] = true;
- for (auto &edge : g[v]){
- if (dist[edge.first] > dist[v] + edge.second) {
- dist[edge.first] = (dist[v] + edge.second);
- par[edge.first] = v;
- }
- }
- }
- if (dist[f] == mnogo){
- //cout << (-1);
- writeInt(-1);
- return 0;
- }
- int c = f;
- stack<int> path;
- while (c != s) {
- path.push(c);
- c = par[c];
- }
- writeInt(s + 1, ' ');
- while (!path.empty()) {
- //cout << path.top() << ' ';
- writeInt(path.top() + 1, ' ');
- path.pop();
- }
- //cout << f + 1;
- //writeInt(f + 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement