Pagini recente » Cod sursa (job #71425) | Cod sursa (job #2313384) | Cod sursa (job #679514) | Cod sursa (job #1284994) | Cod sursa (job #3134090)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
#define MAXN 50010
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
vector<pair<int, int>> G[MAXN];
priority_queue<pair<int, int>> PQ;
int n, m, x, y, c, dist[MAXN], viz[MAXN], nod;
int main()
{
f >> n >> m;
while (m--) {
f >> x >> y >> c;
G[x].push_back({y, c});
}
for (int i = 2; i <= n; ++i) {
dist[i] = INT_MAX;
}
dist[1] = 0;
PQ.push({0, 1});
while (PQ.size()) {
auto aux = PQ.top();
PQ.pop();
int nod = aux.second;
if (!viz[nod]) {
viz[nod] = 1;
for (auto it:G[nod]) {
if (dist[nod] + it.second < dist[it.first]) {
dist[it.first] = dist[nod] + it.second;
PQ.push({-dist[it.first], it.first});
}
}
}
}
for (int i = 2; i <= n; ++i) {
if (dist[i] == INT_MAX) {
dist[i] = 0;
}
g << dist[i] << ' ';
}
return 0;
}