Pagini recente » Cod sursa (job #2033480) | Cod sursa (job #2416315)
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int to;
int cost;
bool operator < (Edge const &other) const {
return cost > other.cost;
}
};
const int MAXN = 5e4, INF = 1e9 + 7;
int dist[1 + MAXN];
vector <Edge> gr[1 + MAXN];
int n;
void dijkstra (int start) {
int i;
priority_queue <Edge> heap; /// it's not an edge i know
heap.push ({start, 0});
for (i = 1; i <= n; i++)
dist[i] = INF;
dist[start] = 0;
while (!heap.empty ()) {
Edge nod = heap.top ();
heap.pop ();
if (dist[nod.to] != nod.cost)
continue;
for (Edge vec : gr[nod.to])
if (dist[vec.to] > dist[nod.to] + vec.cost) {
dist[vec.to] = dist[nod.to] + vec.cost;
heap.push ({vec.to, dist[vec.to]});
}
}
}
int main() {
int m, i, x, y, c;
freopen ("dijkstra.in", "r", stdin);
freopen ("dijkstra.out", "w", stdout);
scanf ("%d%d", &n, &m);
for (i = 1; i <= m; i++) {
scanf ("%d%d%d", &x, &y, &c);
gr[x].push_back ({y, c});
gr[y].push_back ({x, c});
}
dijkstra (1);
for (i = 2; i <= n; i++) {
if (dist[i] == INF)
dist[i] = 0;
printf ("%d ", dist[i]);
}
return 0;
}