Cod sursa(job #2368801)

Utilizator flee123Flee Bringa flee123 Data 5 martie 2019 18:07:00
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.64 kb
#include <bits/stdc++.h>
#define nmax 50002
#define pb push_back
#define infinity 2000000000
using namespace std;

ifstream fin ("dijkstra.in");
ofstream fout("dijkstra.out");
int distante[nmax], nod_plecare, n, fr, a, b, c, i;
struct fun{
    bool operator()(int x, int y)
    {
        return distante[x] > distante[y];
    }
};
struct dublu{
    int x, cost;
};
bool checked[nmax];
vector < dublu > graphs[nmax];
priority_queue <int, vector <int>, fun> coada;

void dijkstra(int nod_start)
{
    for(int i = 1; i <= n; i++)
        distante[i] = infinity;

    distante[nod_start]=0;

    coada.push(nod_start);
    checked[nod_start] = true;

    while(!coada.empty())
    {
        int nodCurent = coada.top();
        coada.pop();

        checked[nodCurent] = false;
        for(size_t i = 0; i < graphs[nodCurent].size(); i++)
        {
            int Vecin = graphs[nodCurent][i].x;
            int Cost = graphs[nodCurent][i].cost;
            if(distante[nodCurent] + Cost < distante[Vecin])
            {
                distante[Vecin] = distante[nodCurent] + Cost;
                if(checked[Vecin] == false)
                {
                    coada.push(Vecin);
                    checked[Vecin] = true;
                }
            }
        }
    }
}

int main()
{
    dublu var;
    fin >> n >> nod_plecare;
    while (fin >> a >> b >> c)
        var.x = b, var.cost = c, graphs[a].pb(var);
    dijkstra(1);
    for(a = 2; a <= n; a++)
    {
        if(distante[a] == infinity)
            fout << 0 << ' ';
        else
            fout << distante[a] << ' ';
    }
    return 0;
}