Pagini recente » Cod sursa (job #1143657) | Cod sursa (job #584995) | Cod sursa (job #792998) | Cod sursa (job #504349) | Cod sursa (job #3167067)
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
#include <utility>
const long long INF = LLONG_MAX;
int main () {
std::ios::sync_with_stdio (NULL);
std::cin.tie (NULL), std::cout.tie (NULL);
int n, m; std::cin >> n >> m;
std::vector<long long> dist(n, INF);
std::vector<int> pred(n, -1), path;
std::priority_queue<std::pair<long long, int>,
std::vector<std::pair<long long, int>>,
std::greater<std::pair<long long, int>>> heap;
std::vector<std::vector<std::pair<int, int>>> graph(n, std::vector<std::pair<int, int>> ());
while (m > 0) {
int u, v, c; std::cin >> u >> v >> c; u -= 1, v -= 1;
graph[u].emplace_back (std::make_pair (v, c));
graph[v].emplace_back (std::make_pair (u, c));
m -= 1;
}
dist[0] = 0;
heap.push (std::make_pair (0, 0));
while (!heap.empty ()) {
int intermediary = heap.top ().second;
int distance = heap.top ().first;
heap.pop ();
if (dist[intermediary] < distance)
continue;
for (auto & it : graph[intermediary])
if (dist[it.first] > dist[intermediary] + it.second) {
dist[it.first] = dist[intermediary] + it.second;
pred[it.first] = intermediary, heap.push (std::make_pair (dist[it.first], it.first));
}
}
if (dist[n - 1] == INF) { std::cout << -1; return 0; }
for (int i = n - 1; i != -1; i = pred[i])
path.emplace_back (i);
for (int i = (int) path.size () - 1; i >= 0; i -= 1)
std::cout << path[i] + 1 << ' ';
return 0;
}