Pagini recente » Cod sursa (job #3204677) | Cod sursa (job #1650005) | Cod sursa (job #1252057) | Cod sursa (job #2037893) | Cod sursa (job #3182945)
#include <bits/stdc++.h>
constexpr static auto inf = (1 << 20lu);
constexpr static auto nmax = 50005u;
static std::priority_queue<std::pair<int, int>> pq;
static std::list<std::pair<int, int>> adj[nmax];
static int n, m, d[nmax];
static std::ofstream out;
static std::ifstream in;
static bool viz[nmax];
auto main(void) -> int {
out.open("dijkstra.out");
in.open("dijkstra.in");
in >> n >> m;
for(int i = 1, a, b, c; i <= m; ++i)
in >> a >> b >> c, adj[a].push_back(std::make_pair(b, c));
for(int i = 1; i <= n; ++i)
d[i] = inf;
d[1] = 0;
pq.push(std::make_pair(0, 1));
while(!pq.empty()) {
const auto x = pq.top(); pq.pop();
if(viz[x.second]) continue;
viz[x.second] = true;
for(auto&& v : adj[x.second]) {
if(d[x.second] + v.second < d[v.first]) {
d[v.first] = d[x.second] + v.second;
pq.push(std::make_pair(-d[v.first], v.first));
}
}
}
for(int i = 2; i <= n; ++i)
out << ((d[i] == inf) ? 0 : d[i]) << " ";
out.close();
in.close();
return 0;
}