Pagini recente » Cod sursa (job #2184689) | Cod sursa (job #135713) | Cod sursa (job #1941030) | Cod sursa (job #2049796) | Cod sursa (job #1906578)
#include <bits/stdc++.h>
using namespace std;
const int nmax = 5e4 + 10;
const int inf = 0x3f3f3f3f;
int n, m;
int D[nmax];
vector < pair < int, int > > g[nmax];
void input() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; ++i) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
g[x].push_back({y, z});
g[y].push_back({x, z});
}
}
void run_dijkstra() {
priority_queue < pair < int, int >, vector < pair < int, int > >, greater < pair < int, int > > > pq;
memset(D, inf, sizeof(D));
D[1] = 0;
pq.push({0, 1});
while (pq.size()) {
int node, cost; tie(cost, node) = pq.top(); pq.pop();
if (cost != D[node]) continue;
for (auto &it: g[node]) {
if (cost + it.second >= D[it.first]) continue;
D[it.first] = cost + it.second;
pq.push({D[it.first], it.first});
}
}
}
void output() {
for (int i = 2; i <= n; ++i)
(D[i] == inf) ? printf("0 ") : printf("%d ", D[i]);
}
int main() {
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
input();
run_dijkstra();
output();
return 0;
}