Cod sursa(job #2869605)

Utilizator Sebi_MafteiMaftei Sebastioan Sebi_Maftei Data 11 martie 2022 18:06:43
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>

using namespace std;

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

struct muchie
{
    int nod, cost;

    bool operator < (const muchie A)const
    {
        return cost > A.cost;
    }
};

int n, m;
long long d[50003];
vector<muchie> h[50003];
bitset<50003> viz;

void Citire()
{
    int x, y, c;
    fin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        fin >> x >> y >> c;
        h[x].push_back({y, c});
    }
}

void Dijkstra()
{
    int nod;

    for (int i = 1; i <= n; i++)
        d[i] = 1e9;

    priority_queue<muchie> q;
    q.push({1, 0});
    d[1] = 0;
    while (!q.empty())
    {
        nod = q.top().nod;
        q.pop();
        if (viz[nod] == 0)
        {
            viz[nod] = 1;
            for (auto w : h[nod])
            {
                if (d[w.nod] > d[nod] + w.cost)
                {
                    d[w.nod] = d[nod] + w.cost;
                    q.push({w.nod, d[w.nod]});
                }
            }
        }
    }
    for (int i = 2; i <= n; i++)
        if (d[i] != 1e9) fout << d[i] << " ";
        else fout << "0 ";
}

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