Pagini recente » Cod sursa (job #1274170) | Cod sursa (job #2111570) | Cod sursa (job #2193793) | Cod sursa (job #2193784) | Cod sursa (job #3142439)
#include <bits/stdc++.h>
#define P pair<int, int>
#define inf 1e7
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, i, x, y, c;
vector<P> l[50002];
priority_queue<P> q;
int d[50002];
static inline void calc() {
int c, x;
while(!q.empty()) {
c = q.top().first;
x = q.top().second;
if(d[x] > -c) d[x] = -c;
q.pop();
if(d[x] >= -c) {
for(auto it : l[x]) {
if(d[it.first] == inf) q.push({c - it.second, it.first});
}
}
}
}
int main() {
fin >> n >> m;
while(fin >> x >> y >> c) l[x].push_back({y, c});
for(i = 1; i <= n; i++) d[i] = inf;
q.push({0, 1});
calc();
for(i = 2; i <= n; i++) {
if(d[i] == inf) fout << "0 ";
else fout << d[i] << " ";
}
return 0;
}