Cod sursa(job #3125431)

Utilizator cristiWTCristi Tanase cristiWT Data 3 mai 2023 09:33:24
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;

struct edge {
    int node, cost;
};

struct T {
    int node, dist;

    bool operator < (T aux) const {
        return this->dist > aux.dist;
    }
};

const int NMAX = 1e5 + 10, mod = 1e9 + 7;
int n, m, d[NMAX];
vector<edge> g[NMAX];
priority_queue<T> pq;

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    freopen("dijkstra.in", "r", stdin);
    freopen("dijkstra.out", "w", stdout);

    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y, cost;
        cin >> x >> y >> cost;
        g[x].push_back({y, cost});
    }

    pq.push({1, 0});
    fill(d + 2, d + 1 + n, 1e9);
    while (!pq.empty()) {
        auto [node, dist] = pq.top();
        pq.pop();
        for (auto [u, cost]: g[node])
            if (d[u] > dist + cost) {
                d[u] = dist + cost;
                pq.push({u, d[u]});
            }
    }
    for (int i = 2; i <= n; i++)
        cout << (d[i] == 1e9 ? 0 : d[i]) << ' ';
}