Pagini recente » Cod sursa (job #394064) | Cod sursa (job #2563556) | Statistici UNIBUC CIR DRAGAN NEACSU (Mieii_Fiorosi) | template/fmi-no-stress-4/footer | Cod sursa (job #2954089)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <set>
int main() {
std::ifstream input("dijkstra.in");
std::ofstream output("dijkstra.out");
int n, m;
input >> n >> m;
std::vector<std::vector<std::pair<int, int>>> graph;
graph.resize(n + 1);
for (int i = 0; i < m; ++i) {
int a, b, c;
input >> a >> b >> c;
graph[a].emplace_back(b, c);
}
std::vector<int> dist(n + 1, INT32_MAX);
dist[1] = 0;
auto cmp = [](const std::pair<int, int> &_lhs, const std::pair<int, int> &_rhs) {
return _lhs.second > _rhs.second;
};
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, decltype(cmp)> queue(cmp);
queue.emplace(1, 0);
while (!queue.empty()) {
int current = queue.top().first;
if (queue.top().second != dist[current]) {
queue.pop();
continue;
}
queue.pop();
for (const auto &x: graph[current]) {
int candidate = x.second + dist[current];
if (dist[x.first] > candidate) {
dist[x.first] = candidate;
queue.emplace(x.first, dist[x.first]);
}
}
}
for (int i = 2; i <= n; ++i) {
if (dist[i] == INT32_MAX) dist[i] = 0;
output << dist[i] << " ";
}
return 0;
}