Pagini recente » Cod sursa (job #2537602) | Cod sursa (job #26220) | Cod sursa (job #2436371) | Cod sursa (job #1307177) | Cod sursa (job #2041932)
#include <bits/stdc++.h>
using namespace std ;
int main(int argc, char const *argv[])
{
ifstream fin ("dijkstra.in") ;
ofstream fout ("dijkstra.out") ;
int n, m;
fin >> n >> m ;
vector < vector <pair <int, int> > > gr (n + 1) ;
while (m --) {
int x, y, cost ;
fin >> x >> y >> cost ;
gr [x].push_back ({y, cost}) ;
}
auto comp = [](pair <int, int> A, pair <int, int> B) {
return A.second > B.second ;
};
priority_queue < pair <int, int>, vector < pair <int, int> >, decltype (comp)> H (comp) ;
vector <int> dist (n + 1, 1<< 29) ;
dist [1] = 0 ;
H.push({1, 0}) ;
while (!H.empty()) {
auto cur = H.top () ;
H.pop () ;
auto nod = cur.first ;
auto cost = cur.second ;
if (dist [nod] != cost) continue ;
for (auto &x : gr [nod]) {
if (dist [x.first] > dist [nod] + x.second) {
dist [x.first] = dist [nod] + x.second ;
H.push ({x.first, dist [x.first]}) ;
}
}
}
for (int i = 2 ; i <= n ; ++ i) {
fout << ((dist [i] >= (1 << 29)) ? 0 : dist [i]) << ' ' ;
}
return 0;
}