Cod sursa(job #2905056)

Utilizator YosifIosif Andrei Stefan Yosif Data 19 mai 2022 10:28:52
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 6.17 kb
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

class input {
private:
    FILE* fin;
    char* t;
    int sp;

    char read()
    {
        if (sp == 10000)
        {
            fread(t, 1, 10000, fin);
            sp = 0;
            return t[sp++];
        }
        else
            return t[sp++];
    }
public:
    input(const char* name)
    {
        fin = fopen(name, "r");
        sp = 10000;
        t = new char[10000]();
    }

    void close()
    {
        fclose(fin);
    }

    void open(const char* name)
    {
        fin = fopen(name, "r");
        sp = 10000;
        t = new char[10000]();
    }

    input& operator >> (int& n)
    {
        char c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;

        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (c != '\0' && isdigit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    input& operator >> (char& s)
    {
        char c = read();

        while (c != '\0' && c == '\n')
            c = read();

        s = c;

        return *this;
    }

    input& operator >> (long long& n)
    {
        char c = read();

        while (c == ' ' || c == '\n')
            c = read();

        n = 0;

        int sng = 1;

        if (c == '-')
            sng = -1, c = read();

        while (c != '\0' && isdigit(c))
            n = n * 10 + (c - '0'), c = read();

        n *= sng;

        return *this;
    }

    void getline(string& s)
    {
        char c = read();
        s = "";

        while (c != '\0' && c != '\n')
            s += c, c = read();
    }
};
class output{
private:
    FILE* fout;
    char* t;
    int sp;

    void write(char c)
    {
        if (sp == 5000)
        {
            fwrite(t, 1, 5000, fout);
            sp = 0;
            t[sp++] = c;
        }
        else
            t[sp++] = c;
    }
    
public:
    output(const char* name)
    {
        fout = fopen(name, "w");
        sp = 0;
        t = new char[5000]();
    }
    ~output()
    {
        fwrite(t, 1, sp, fout);
    }

    output& operator << (int n)
    {
        if (n < 0)
        {
            write('-');
            n *= -1;
        }
        if (n <= 9)
            write(char(n + '0'));
        else
        {
            (*this) << (n / 10);
            write(char(n % 10 + '0'));
        }

        return *this;
    }

    output& operator << (char c)
    {
        write(c);

        return *this;
    }

    output& operator << (const char* s)
    {
        int i = 0;

        while (s[i] != '\0')
            write(s[i++]);

        return *this;
    }

    output& operator << (long long n)
    {
        if (n < 0)
        {
            write('-');
            n *= -1;
        }
        if (n < 10)
            write(char(n + '0'));
        else
        {
            (*this) << (n / 10);
            write(char(n % 10 + '0'));
        }

        return *this;
    }

    output& operator << (string s)
    {
        for (auto i : s)
            write(i);

        return *this;
    }

    void precizion(double x, int nr)
    {
        int p = floor(x);

        *this << p;

        if (nr == 0)
            return;

        write('.');

        for (int i = 1; i <= nr; i++)
        {
            x -= floor(x);
            x *= 10;

            write(int(x) + '0');
        }
    }
};
class heap {
private:
    struct grup{
        int nod;
        ll val;
    };

    int sz;
    int *index;
    vector<grup> h;

    void up(int k)
    {
        while (k > 1)
        {
            int aux = k / 2;

            if (h[aux].val > h[k].val)
            {
                swap(index[h[aux].nod], index[h[k].nod]);
                swap(h[aux], h[k]);
                k = aux;
            }
            else
                break;
        }
    }

    void down(int k)
    {
        while (2 * k <= sz)
        {
            int p = 2 * k;

            if (p + 1 <= sz && h[p + 1].val >= h[p].val)
                p++;

            if (h[p].val < h[k].val)
            {
                swap(index[h[p].nod], index[h[k].nod]);
                swap(h[p], h[k]);
                k = p;
            }
            else
                break;
        }
    }

public:
    heap()
    {
        h = vector<grup>(50001);
        index = new int[50001]();
        sz = 0;
    }

    void update(int nod, ll val)
    {
        int ind = index[nod];
        h[ind].val = val;

        up(ind);
        down(ind);
    }

    bool find(int nod)
    {
        return index[nod];
    }

    void add(int nod, ll val)
    {
        grup a;
        
        a.nod = nod;
        a.val = val;

        h[++sz] = a;
        index[nod] = sz;

        up(sz);
    }

    void pop(int nod)
    {
        int ind = index[nod];

        index[h[sz].nod] = ind;
        index[nod] = 0;
        h[ind] = h[sz];
        sz--;

        down(ind);
    }

    bool empty()
    {
        return sz == 0;
    }

    grup top()
    {
        return h[1];
    }

    ll val_nod(int nod)
    {
        return h[index[nod]].val;
    }
};

input fin("dijkstra.in");
output fout("dijkstra.out");
const long long inf = 1000000000000000000;

vector<vector<pair<int, int>>> g(50001);
int n, m;
heap H;

int main()
{
    fin >> n >> m;

    int i, j, c;
    ll d[50001];

    for (int k = 1; k <= m; k++)
    {
        fin >> i >> j >> c;

        g[i].push_back(make_pair(j, c));
    }

    H.add(1, 0);
    d[1] = 0;

    for (int i = 2; i <= n; i++)
        H.add(i, inf);

    while (!H.empty())
    {
        int nod = H.top().nod;
        d[nod] = H.top().val;

        H.pop(nod);

        for (auto i : g[nod])
            if (H.find(i.first) && H.val_nod(i.first) > d[nod] + i.second)
                H.update(i.first, d[nod] + i.second);
    }


    for (int i = 2; i <= n; i++)
        fout << d[i] << ' ';
   
    return 0;
}