Cod sursa(job #3182935)

Utilizator npc3233npc npc npc3233 Data 10 decembrie 2023 12:13:52
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>

constexpr static auto inf = (1 << 20lu);
constexpr static auto nmax = 50005u;

static int n, m, d[nmax], tata[nmax], st;
static std::priority_queue<std::pair<int, int>> pq;
static std::list<std::pair<int, int>> adj[nmax];
static std::ofstream out;
static std::ifstream in;

auto main(void) -> int {
    out.open("dijkstra.out");
    in.open("dijkstra.in");
    in >> n >> m;
    for(int i = 1, a, b, c; i <= m; ++i)
        in >> a >> b >> c, adj[a].push_back(std::make_pair(b, c)), adj[b].push_back(std::make_pair(a, c));

    for(int i = 1; i <= n; ++i)
        d[i] = inf, tata[i] = 0;

    d[1] = 0;
    for(int i = 1; i <= n; ++i)
        pq.push(std::make_pair(-d[i], i));

    while(!pq.empty()) {
        const auto x = pq.top(); pq.pop();
        for(auto&& v : adj[x.second]) {
            if(d[x.second] + v.second < d[v.first]) {
                d[v.first] = d[x.second] + v.second;
                tata[v.first] = x.second;
            }
        }
    }

    st = 1;
    for(int i = 2; i <= n; ++i)
        out << d[i] << " ";

    out.close();
    in.close();
    return 0;
}