Pagini recente » Cod sursa (job #1531008) | Monitorul de evaluare | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #1427086)
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 50000;
const int INF = 1 << 30;
struct muchie {
int nod, cost;
bool operator> (const muchie& y) const {
return cost > y.cost;
}
};
struct {
vector<muchie> vecini;
int dist = INF;
} nod[NMAX];
int main() {
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
int N, M;
scanf("%d %d\n", &N, &M);
for (int i = 0; i < M; i++) {
int v, u, c;
scanf("%d %d %d\n", &v, &u, &c);
--v, --u;
nod[v].vecini.push_back({u, c});
}
priority_queue<muchie, vector<muchie>, greater<muchie>> coada;
coada.push({0, 0});
nod[0].dist = 0;
for (; !coada.empty(); coada.pop()) {
int v = coada.top().nod;
if (coada.top().cost > nod[v].dist) continue;
// printf("%d %d\n", v, nod[v].dist);
for (auto m: nod[v].vecini) {
int u = m.nod, dist = m.cost + nod[v].dist;
if (dist < nod[u].dist) {
nod[u].dist = dist;
coada.push({u, dist});
}
}
}
for (int v = 1; v < N; v++) {
int c = nod[v].dist == INF ? 0 : nod[v].dist;
printf("%d ", c);
}
return 0;
}