Cod sursa(job #3291796)

Utilizator radugheoRadu Mihai Gheorghe radugheo Data 5 aprilie 2025 18:03:36
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 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<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(1);
    while (!s.empty()){
        auto node = s.begin();
        s.erase(node);
        for (auto it : L[*node]){
            if (d[it.first] > it.second + d[*node]){
                d[it.first] = it.second + d[*node];
                s.insert(it.first);
            }
        }
    }
    for (int i=2; i<=n; i++){
        fout << d[i] << ' ';
    }
    return 0;
}