Pagini recente » Cod sursa (job #2286623) | Cod sursa (job #606379) | Cod sursa (job #135316) | Cod sursa (job #1013426) | Cod sursa (job #3200510)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int inf = 250002;
int n, m, i, x, y, c;
set<pair<int, int>> a[50002];
int d[50002];
static inline void Calc() {
priority_queue<pair<int, int>> q;
for(i = 1; i <= n; i++) d[i] = inf;
q.push({0, 1});
while(!q.empty()) {
int c = q.top().first;
int x = q.top().second;
q.pop();
if(d[x] > -c) d[x] = -c;
if(d[x] >= -c) {
for(auto it : a[x]) {
if(d[it.first] == inf) q.push({c - it.second, it.first});
}
}
}
}
int main() {
fin >> n >> m;
for(i = 1; i <= m; i++) {
fin >> x >> y >> c;
a[x].insert({y, c});
}
Calc();
for(i = 2; i <= n; i++) {
if(d[i] == inf) fout << "0 ";
else fout << d[i] << " ";
}
return 0;
}