Pagini recente » Cod sursa (job #52087) | Cod sursa (job #2129804) | Cod sursa (job #1568636) | Cod sursa (job #2834261) | Cod sursa (job #2087698)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMax = 5e4 + 50;
bool viz[NMax];
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(viz[it.first] == false) {
viz[it.first] = true;
dist[it.first] = dist[node] + it.second;
PQ.push(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});
}
viz[1] = true;
Dijkstra();
for(int i = 2; i <= n; ++i) fout << dist[i] << " ";
return 0;
}