Pagini recente » Cod sursa (job #1482273) | Cod sursa (job #733870) | Cod sursa (job #1777806) | Cod sursa (job #1895967) | Cod sursa (job #3125918)
/*
* Lefter Sergiu - 04/05/2023
*/
#include <fstream>
#include <vector>
#include <climits>
#include <queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int N = 5e4;
const int INF = INT_MAX;
struct succesor {
int vf, c;
};
vector <succesor> s[N+1];
bool selectat[N+1];
int n, m, d[N+1];
void dijkstra(int x0) {
priority_queue <pair <int, int>> pq;
for (int i = 1; i <= n; ++i) {
d[i] = INF;
selectat[i] = false;
}
d[x0] = 0;
pq.push({0, x0});
while (!pq.empty()) {
int x = pq.top().second;
pq.pop();
if (selectat[x]) {
continue;
}
selectat[x] = true;
for (auto p: s[x]) {
int y = p.vf;
int c = p.c;
if (selectat[y]) { //daca a fost selectat nu il mai bagam in seama
continue;
}
if (d[x] + c < d[y]) {
d[y] = d[x] + c;
pq.push({-d[y], y});
}
}
}
}
int main() {
in >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y, c;
in >> x >> y >> c;
s[x].push_back((succesor){y, c});
}
dijkstra(1);
for (int i = 2; i <= n; ++i) {
if (d[i] == INF) {
d[i] = -1;
}
out << d[i] << " ";
}
in.close();
out.close();
return 0;
}