Pagini recente » Cod sursa (job #2981111) | Cod sursa (job #2405426) | Cod sursa (job #2780994) | Cod sursa (job #2873013) | Cod sursa (job #2722482)
#include <fstream>
#define mF "dijkstra"
std::ifstream in(mF ".in");
std::ofstream out(mF ".out");
#include <vector>
#include <utility>
#define x first
#define y second
constexpr int N = 50001; int D[N]; std::vector<std::pair<int, int>> L[N];
#include <queue>
#include <algorithm>
int main()
{
int n, m; in >> n >> m; while (m--) {int a, b, c; in >> a >> b >> c; L[a].push_back({c, b});}
std::fill(D + 2, D + n + 1, (1 << 31) - 1); std::priority_queue<std::pair<int, int>> Q; for (Q.push({0, 1}); Q.size();)
{int c = -Q.top().x, t = Q.top().y; Q.pop(); if (c == D[t]) for (auto p: L[t]) if (c + p.x < D[p.y]) D[p.y] = c + p.x, Q.push({-D[p.y], p.y});}
for (int i = 2; i <= n; i++) out << D[i] << ' ';
}