Cod sursa(job #2175369)

Utilizator UWantMyNameGheorghe Vlad Camil UWantMyName Data 16 martie 2018 16:53:49
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>
#define in "dijkstra.in"
#define out "dijkstra.out"
#define Nmax 50003
#define oo (1 << 30)
using namespace std;
ifstream fin(in);
ofstream fout(out);

int n,m;
int d[Nmax];
bitset <Nmax> viz;
vector <pair<int,int> > L[Nmax];
priority_queue <pair<int,int> > q;

void Read()
{
    int i,x,y,cost;

    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        fin >> x >> y >> cost;
        L[x].push_back({y,cost});
        L[y].push_back({x,cost});
    }
}

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

    /// nodul de start este 1
    fill(d + 1, d + n + 1, oo);
    d[1] = 0;
    q.push({0,1});
    while (!q.empty())
    {
        k = q.top().second;
        q.pop();

        if (viz[k] == 0)
        {
            viz[k] = 1;
            for (auto i : L[k])
            {
                nod = i.first;
                cost = i.second;
                if (d[nod] > d[k] + cost)
                {
                    d[nod] = d[k] + cost;
                    q.push({-d[nod],nod});
                }
            }
        }
    }
}

void Afis()
{
    int i;

    for (i = 2; i <= n; i++)
        fout << d[i] << " ";
    fout << "\n";
}

int main()
{
    Read();
    Dijkstra();
    Afis();

    fin.close();
    fout.close();
    return 0;
}