Cod sursa(job #3133018)

Utilizator tomaionutIDorando tomaionut Data 24 mai 2023 20:24:48
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.53 kb
#include <bits/stdc++.h>

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

template<class T>
class priorityQueue {
private:
    vector<T> coada;

    void heapifyUp(int x) {
        int tata = (x - 1) / 2;
        while (x > 0 && coada[x] < coada[tata]) {
            swap(coada[x], coada[tata]);
            x = tata;
            tata = (x - 1) / 2;
        }
    }

    void heapifyDown(int x) {
        int copilStanga = 2 * x + 1;
        int copilDreapta = 2 * x + 2;
        int copilMin = x;

        if (copilStanga < coada.size() && coada[copilStanga] < coada[copilMin])
            copilMin = copilStanga;

        if (copilDreapta < coada.size() && coada[copilDreapta] < coada[copilMin])
            copilMin = copilDreapta;

        if (copilMin != x) {
            swap(coada[x], coada[copilMin]);
            heapifyDown(copilMin);
        }
    }

public:
    bool empty() const
    {
        return coada.empty();
    }

    int size() const
    {
        return coada.size();
    }

    void push(T& value)
    {
        coada.push_back(value);
        heapifyUp(coada.size() - 1);
    }

    T pop()
    {
        if (this->empty())
            exit(0);
        if (!this->empty()) {
            T minVal = coada.front();
            swap(coada.front(), coada.back());
            coada.pop_back();
            heapifyDown(0);

            return minVal;
        }
    }

    T top() const
    {
        if (this->empty())
            exit(0);
        if (!this->empty()) {
            return coada.front();
        }
    }

    void print() const
    {
        for (auto i : coada)
            cout << i << " ";
        cout << '\n';
    }
};

struct Nod
{
    int nod, cost;
    bool operator<(Nod A) const
    {
        return A.cost > cost;
    }
};

int s, n, m, viz[100003], d[100003];

vector<Nod> L[100003];
priorityQueue<Nod> q;
int oo = 2000000099;

void Citire()
{
    int i, j, k, cost;
    Nod e;
    fin >> n >> m;
    for (k = 1; k <= m; k++)
    {
        fin >> i >> j >> cost;
        e.cost = cost; e.nod = j;
        L[i].push_back(e);
        e.nod = i;
        //L[j].push_back(e);
    }
}

/// O(n * log n)
void Dijkstra(int s)
{
    int k, cost, i;
    Nod w;
    for (i = 1; i <= n; ++i)
    {
        d[i] = oo;
        viz[i] = 0;
    }
    d[s] = 0;
    w.cost = 0; w.nod = s;
    q.push(w);
    while (!q.empty())
    {
        /// extragem nodul de cost minim
        k = q.top().nod;
        cout << k << " ";
        q.pop();
        if (viz[k] == 0)
        {
            viz[k] = 1;
            for (auto e : L[k])
            {
                i = e.nod;
                cost = e.cost;
                if (d[i] > d[k] + cost)
                {
                    d[i] = d[k] + cost;
                    w.cost = d[i];
                    w.nod = i;
                    q.push(w);
                }
            }
        }
    }
}

void Afisare()
{
    for (int i = 2; i <= n; i++)
        if (d[i] == oo) fout << "-1 ";
        else fout << d[i] << " ";
}

int main()
{
    Citire();
    Dijkstra(1);
    Afisare();
    /**
        Nod e;
        e.cost = 52; e.nod = 1; q.push(e);
        e.cost = 23; e.nod = 2; q.push(e);
        e.cost = 100; e.nod = 3; q.push(e);
        e.cost = 64; e.nod = 4; q.push(e);

        while (!q.empty())
        {
            cout << q.top().cost << " ";
            q.pop();
        }
    */
    return 0;
}