Cod sursa(job #2428796)

Utilizator calinfloreaCalin Florea calinflorea Data 6 iunie 2019 15:21:32
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <bits/stdc++.h>
#define NMax 50006
#define oo (1 << 30)

using namespace std;

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

int n, m;
vector <pair <int, int> > L[NMax];
priority_queue <pair <int, int> > Q;
int drum[NMax];
void Read ()
{
    int x, y, cost;

    fin >> n >> m;

    for (int i = 1 ; i <= m; i++)
    {
        fin >> x >> y >> cost;

        L[x].push_back ({y, cost});

    }
}

void Initilizare ()
{
    for (int i = 1; i <= n; i++)
        drum[i] = oo;
}

void Dijkstra ()
{
    int i, nod, cost;

    Q.push ({0, 1});
    Initilizare();
    drum[1] = 0;

    while (!Q.empty())
    {
        i = Q.top().second;
        Q.pop();

        for (int j = 0; j < L[i].size(); j++)
        {
            nod = L[i][j].first;
            cost = L[i][j].second;

            if (drum[nod] > drum[i] + cost)
            {
                drum[nod] = drum[i] + cost;
                Q.push ({-drum[nod], nod});
            }
        }
    }

    for (int i = 2; i <= n; i++)
        fout << drum[i] << " ";

}
int main()
{
    Read();
    Dijkstra();
    return 0;
}