Cod sursa(job #2399002)

Utilizator valentinoMoldovan Rares valentino Data 6 aprilie 2019 17:29:12
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <string>

#define Nmax 50001
#define inf 0x3f3f3f3f

using namespace std;

int main()
{
    ifstream f("dijkstra.in");
    ofstream g("dijkstra.out");

    int n, m, dist[Nmax], used[Nmax], x, y, c;
    priority_queue<pair<int, int>, vector<pair< int, int>>, greater<pair< int, int >>> heap;
    vector<pair<int, int>> graf[Nmax];

    f >> n >> m;
    while(m--){
        f >> x >> y >> c;
        graf[x].push_back({y, c});
    }

    for(int i = 1; i <= n; ++i){
        dist[i] = inf;
        used[i] = 0;
    }
    heap.push({0, 1});
    dist[1] = 0;
    used[1] = 1;

    while(!heap.empty()){
        int node = heap.top().second;
        heap.pop();
        for(auto v : graf[node]){
            if(dist[node] + v.second < dist[v.first]){
                dist[v.first] = dist[node] + v.second;
                if(!used[v.first]){
                    heap.push({dist[v.first], v.first});
                    used[v.first] = 1;
                }
            }
        }
    }
    for(int i = 2; i <= n; ++i)
        g << dist[i] << ' ';
}