Cod sursa(job #3291797)

Utilizator radugheoRadu Mihai Gheorghe radugheo Data 5 aprilie 2025 18:12:11
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
#include <vector>
#include <set>
#include <climits>

using namespace std;

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

int n, m, x, y, c;
int f[50005], d[50005];
vector<pair<int, int> > L[50005];
set<pair<int, int>> s;

int main(){
    fin >> n >> m;
    for (int i=0; i<m; i++){
        fin >> x >> y >> c;
        L[x].push_back({y,  c});
    }
    for (int i=0; i<=n; i++){
        d[i] = INT_MAX;
    }
    d[1] = 0;
    s.insert({0, 1});
    while (!s.empty()){
        auto node = s.begin();
        s.erase(s.begin());
        for (auto it : L[(*node).second]){
            int neighbour = it.first;
            int cost = it.second;
            if (d[neighbour] > cost + d[(*node).second]){
                s.erase({d[neighbour], neighbour});
                d[neighbour] = cost + d[(*node).second];
                s.insert({d[neighbour], neighbour});
            }
        }
    }
    for (int i=2; i<=n; i++){
        fout << d[i] << ' ';
    }
    return 0;
}