Cod sursa(job #3135247)

Utilizator NoRules123Osadici Darius Bogdan NoRules123 Data 2 iunie 2023 14:08:51
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 50005;


vector<pair<int, int>> adj[NMAX + 5];
priority_queue<pair<int, int>> pq;

int dist[NMAX + 5];
int viz[NMAX + 5];

int main(){
    int n, m;
    fin>>n>>m;
    for(int i = 1; i <= m; i++){
        int x, y, cost;
        fin>>x>>y>>cost;
        adj[x].push_back({y, cost});
    }
    for(int i = 2; i <= n; i++){
        dist[i] = INT_MAX;
    }
    dist[1] = 0;
    pq.push({0, 1});
    while (!pq.empty()){
        auto aux = pq.top();
        pq.pop();
        int nod = aux.second;
        if(!viz[nod]){
            viz[nod] = 1;
            for(auto it: adj[nod]){
                if(dist[nod] + it.second < dist[it.first]){
                    dist[it.first] = dist[nod] + it.second;
                    pq.push({-dist[it.first], it.first});
                }
            }
        }
    }
    for(int i = 2; i <= n; i++){
        if(dist[i] == INT_MAX){
            dist[i] = 0;
        }
        fout<<dist[i]<<" ";
    }
    return 0;
}