Pagini recente » Cod sursa (job #2414223) | Cod sursa (job #1864302) | Cod sursa (job #1019077) | Cod sursa (job #1118470) | Cod sursa (job #2132648)
#include <bits/stdc++.h>
using namespace std;
int n, m, dist[50010];
vector <pair<int,int> > V[50010];
set <pair<int,int> > S;
void dijkstra(){
S.insert({0,1});
while (S.size()){
int nod = S.begin()->second, c = S.begin()->first;
S.erase(S.begin());
for (auto it: V[nod]){
if (c + it.second < dist[it.first]){
if (dist[it.first] != (1<<30)){
S.erase(S.find({dist[it.first], it.first}));
}
dist[it.first] = c + it.second;
S.insert({dist[it.first], it.first});
}
}
}
}
int main(){
ifstream cin ("dijkstra.in");
ofstream cout ("dijkstra.out");
cin >> n >> m;
for (int i=1; i<=m; i++){
int x, y, cost;
cin >> x >> y >> cost;
V[x].push_back({y, cost});
}
for (int i=2; i<=n; i++) dist[i] = (1<<30);
dijkstra();
for (int i=2; i<=n; i++) cout << (dist[i] == (1<<30) ? 0 : dist[i]) << " ";
return 0;
}