#include <iostream>
#define FIN "dijkstra.in"
#define FOUT "dijkstra.out"
#include <vector>
#include <queue>
#include <limits>
#define MAX_NODES 50005
#define INF std::numeric_limits<int>::max()
using namespace std;
vector<int> V[MAX_NODES];
vector<int> C[MAX_NODES];
int distMin[MAX_NODES];
void readGraph(int& nodes, int& edges) {
//freopen(FIN, "r", stdin);
cin >> nodes >> edges;
for (int edge = 1; edge <= edges; ++edge) {
int x, y, cost;
cin >> x >> y >> cost;
V[x].push_back(y);
C[x].push_back(cost);
}
}
void dijkstra(int startNode, int nodes) {
for (int i = 2; i <= nodes; ++i)
distMin[i] = INF;
distMin[startNode] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, startNode});
while (!pq.empty()) {
int currDist = pq.top().first;
int currNode = pq.top().second;
pq.pop();
if (currDist > distMin[currNode]) continue;
for (int i = 0; i < V[currNode].size(); ++i) {
int nextNode = V[currNode][i];
int cost = C[currNode][i];
if (distMin[nextNode] > distMin[currNode] + cost) {
distMin[nextNode] = distMin[currNode] + cost;
pq.push({distMin[nextNode], nextNode});
}
}
}
}
void printShortestDistances(int nodes) {
// freopen(FOUT, "w", stdout);
for (int i = 2; i <= nodes; ++i) {
cout << (distMin[i] < INF ? distMin[i] : 0) << " ";
}
cout << endl;
}
int main() {
int nodes, edges;
readGraph(nodes, edges);
dijkstra(1, nodes);
printShortestDistances(nodes);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojZGVmaW5lIEZJTiAiZGlqa3N0cmEuaW4iCiNkZWZpbmUgRk9VVCAiZGlqa3N0cmEub3V0IgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxsaW1pdHM+CgojZGVmaW5lIE1BWF9OT0RFUyA1MDAwNQojZGVmaW5lIElORiBzdGQ6Om51bWVyaWNfbGltaXRzPGludD46Om1heCgpCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdmVjdG9yPGludD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ICAgICAgICAgICBpbnQgbmV4dE5vZGUgPSBWW2N1cnJOb2RlXVtpXTsKICAgICAgICAgICAgaW50IGNvc3QgPSBDW2N1cnJOb2RlXVtpXTsKICAgICAgICAgICAgaWYgKGRpc3RNaW5bbmV4dE5vZGVdID4gZGlzdE1pbltjdXJyTm9kZV0gKyBjb3N0KSB7CiAgICAgICAgICAgICAgICBkaXN0TWluW25leHROb2RlXSA9IGRpc3RNaW5bY3Vyck5vZGVdICsgY29zdDsKICAgICAgICAgICAgICAgIHBxLnB1c2goe2Rpc3RNaW5bbmV4dE5vZGVdLCBuZXh0Tm9kZX0pOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIHByaW50U2hvcnRlc3REaXN0YW5jZXMoaW50IG5vZGVzKSB7CiAgLy8gIGZyZW9wZW4oRk9VVCwgInciLCBzdGRvdXQpOwogICAgZm9yIChpbnQgaSA9IDI7IGkgPD0gbm9kZXM7ICsraSkgewogICAgICAgIGNvdXQgPDwgKGRpc3RNaW5baV0gPCBJTkYgPyBkaXN0TWluW2ldIDogMCkgPDwgIiAiOwogICAgfQogICAgY291dCA8PCBlbmRsOwp9CgppbnQgbWFpbigpIHsKICAgIGludCBub2RlcywgZWRnZXM7CiAgICByZWFkR3JhcGgobm9kZXMsIGVkZ2VzKTsKICAgIGRpamtzdHJhKDEsIG5vZGVzKTsKICAgIHByaW50U2hvcnRlc3REaXN0YW5jZXMobm9kZXMpOwogICAgcmV0dXJuIDA7Cn0=