Pagini recente » Cod sursa (job #534130) | Cod sursa (job #986459) | Cod sursa (job #957280) | Cod sursa (job #2819129) | Cod sursa (job #3335343)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define N 50008
#define inf 2e9
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n;
vector<pair<int, int>> g[N];
int D[N];
bool S[N];
struct cmp {
bool operator()(int a, int b) {
return D[a] > D[b];
}
};
priority_queue<int, vector<int>, cmp> q;
void Citire() {
int m;
int x, y, c;
fin >> n >> m;
for ( int i = 1; i <= m; i++ ) {
fin >> x >> y >> c;
g[x].push_back({y, c});
}
}
void Dijkstra(int x) {
for ( int i = 1; i <= n; i++ )
D[i] = inf;
D[x] = 0;
S[x] = true;
q.emplace(x);
while (!q.empty()) {
int x = q.top();
q.pop();
for ( auto y : g[x] ) {
if (D[y.first] <= D[x] + y.second) continue;
D[y.first] = D[x] + y.second;
if (!S[y.first]) {
S[y.first] = true;
q.emplace(y.first);
}
}
}
}
int main() {
Citire();
Dijkstra(1);
for ( int i = 2; i <= n; i++ )
if (D[i] != inf) fout << D[i] << " ";
return 0;
}