Cod sursa(job #3291799)

Utilizator radugheoRadu Mihai Gheorghe radugheo Data 5 aprilie 2025 18:16:11
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 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 first_element = s.begin();
        s.erase(s.begin());
        int node = (*first_element).second;
        for (auto it : L[node]){
            int neighbour = it.first;
            int cost = it.second;
            if (d[neighbour] > cost + d[node]){
                s.erase({d[neighbour], neighbour});
                d[neighbour] = cost + d[node];
                s.insert({d[neighbour], neighbour});
            }
        }
    }
    for (int i=2; i<=n; i++){
        if (d[i] == INT_MAX){
            fout << 0 << ' ';
        }
        else{
            fout << d[i] << ' ';
        }
    }
    return 0;
}