Pagini recente » vacanta_11_5 | Cod sursa (job #3271122) | Cod sursa (job #2708194) | Cod sursa (job #2763910) | Cod sursa (job #3251530)
#include <bits/stdc++.h>
using namespace std;
string file="dijkstra";
ifstream fin(file+".in");
ofstream fout(file+".out");
vector < pair < int, int > > v[50005];
int n, m, x, y, c;
set < pair < int , int > > s; /// retinem perechile {dist, nod} => in s.begin() avem nodul cu dist minima
int dist[50005];
int inf = 1000000000;
int main() {
fin >> n >> m;
for (int i=1;i<=m;i++) {
fin >> x >> y >> c;
v[x].push_back({y, c});
/// pt neorientat facem si: v[y].push_back({x, c});
}
dist[1] = 0;
for (int i=2;i<=n;i++) dist[i] = inf;
s.insert({0, 1});
while (!s.empty()) {
int nod = s.begin()->second;
s.erase(s.begin());
for (auto ed: v[nod]) {
if (dist[ed.first] > dist[nod] + ed.second) {
s.erase({dist[ed.first], ed.first}); /// stergem nodul cu distanta veche
dist[ed.first] = dist[nod] + ed.second; /// actualizam dist
s.insert({dist[ed.first], ed.first}); /// il inseram din nou -> el va fi ordonat corespunzator in set
}
}
}
for (int i=2;i<=n;i++) {
if (dist[i] == inf) fout << 0 << " ";
else fout << dist[i] << " ";
}
return 0;
}