Pagini recente » Cod sursa (job #902841) | Cod sursa (job #1614400) | Cod sursa (job #1724479) | Cod sursa (job #435641) | Cod sursa (job #1443858)
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int nmx = 50005;
const int inf = 0x3f3f3f3f;
int n, dist[nmx];
vector <pair <int,int> > g[nmx];
priority_queue <pair <int,int> > q;
inline void citire() {
int m, nod1, nod2, cost;
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; ++i) {
scanf("%d %d %d", &nod1, &nod2, &cost);
g[nod1].push_back(make_pair(cost, nod2));
}
}
inline void calcul() {
memset(dist, inf, sizeof(dist));
dist[1] = 0;
q.push(make_pair(0,1));
int nod, cost, x;
while(not q.empty()) {
cost = q.top().first;
nod = q.top().second;
q.pop();
x = g[nod].size();
for(int i = 0; i < x; ++i) {
if(dist[g[nod][i].second] > cost + g[nod][i].first) {
dist[g[nod][i].second] = cost + g[nod][i].first;
q.push(make_pair(cost + g[nod][i].first, g[nod][i].second));
}
}
}
}
inline void afish() {
for(int i = 2; i <= n; ++i)
if(dist[i] == inf)
printf("0 ");
else
printf("%d ", dist[i]);
}
int main() {
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
citire();
calcul();
afish();
return 0;
}