Pagini recente » Cod sursa (job #2840663) | Cod sursa (job #2535309) | Cod sursa (job #2989221) | Cod sursa (job #2807991) | Cod sursa (job #3344559)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define int long long
#define cin fin
#define cout fout
struct el {
int nod,cost;
bool operator <(const el &other) const {
return cost > other.cost;
}
};
vector<vector<el>> adj;
vector<int> dist;
priority_queue<el> pq;
void dijkstra(int start) {
pq.push({start, 0});
dist[start] = 0;
while(!pq.empty()) {
int u = pq.top().nod;
int u_c = pq.top().cost;
pq.pop();
if(u_c > dist[u]) continue;
for(auto e : adj[u]) {
int v = e.nod;
int v_c = e.cost;
if(dist[u] + v_c < dist[v]) {
dist[v] = dist[u] + v_c;
pq.push({v, dist[v]});
}
}
}
}
int32_t main()
{
int n, m;
cin >> n >> m;
adj.resize(m + 1);
for(int i = 1; i <= m; i++) {
int x, y, c;
cin >> x >> y >> c;
adj[x].push_back({y,c});
}
dist.assign(n + 1, 1e18);
dijkstra(1);
for(int i = 2; i <= n; i++) {
if(dist[i] == 1e18) cout << 0 << " ";
else cout << dist[i] << " ";
}
}