Pagini recente » Cod sursa (job #2985099) | Cod sursa (job #2384277) | Cod sursa (job #2147681) | Cod sursa (job #2724580) | Cod sursa (job #2087707)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMax = 5e4 + 50;
int dist[NMax];
vector < pair < int, int > > G[NMax];
struct cmp {
bool operator() (int a, int b) {
return dist[a] > dist[b];
}
};
void Dijkstra() {
priority_queue < int, vector < int >, cmp > PQ;
PQ.push(1);
while((int)PQ.size()) {
int node = PQ.top(); PQ.pop();
for(const auto it: G[node]) {
if(dist[node] + it.second < dist[it.first]) {
dist[it.first] = dist[node] + it.second;
PQ.emplace(it.first);
}
}
}
}
int main() {
int n, m;
fin >> n >> m;
for(int i = 1; i <= m; ++i) {
int a, b, c;
fin >> a >> b >> c;
G[a].push_back({b, c});
}
for(int i = 2; i <= n; ++i) dist[i] = INT_MAX;
Dijkstra();
for(int i = 2; i <= n; ++i) {
if(dist[i] == INT_MAX) {
fout << "0 ";
} else {
fout << dist[i] << " ";
}
}
return 0;
}