Cod sursa(job #3381282)

Utilizator aspaNadu Tudor aspa Data 5 decembrie 2026 20:20:41
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb

#include <cstdint>
#include <fstream>
#include <stack>
#include <map>
#include <vector>
using namespace std;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

vector<int> A[50001];
map<pair<int, int>, int> W;
int d[50001];

int main() {
    int N, M, x, y, w;
    fin >> N >> M;

    fill(d+2, d+50000, INT16_MAX);

    for (int i = 0; i < M; i++) {
        fin >> x >> y >> w;
        A[x].push_back(y);
        W[{x, y}] = w;
    }

    stack<int> st;
    st.push(1);
    int k;

    while (!st.empty()) {
        k = st.top();
        st.pop();
        for (auto x : A[k]) {
            if (d[x] > d[k] + W[{k, x}]) {
                d[x] = d[k] + W[{k, x}];
                st.push(x);
            }
        }
    }

    for (int i = 2; i <= N; i++) {
        fout << d[i] << " ";
    }


    return 0;
};