Cod sursa(job #2368784)

Utilizator flee123Flee Bringa flee123 Data 5 martie 2019 18:00:56
Problema Algoritmul lui Dijkstra Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 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)
{
    unsigned contor, marime;
    for(i  = 1; i <= n; i++)
        distante[i] = infinity;
    distante[nod_start] = 0;
    coada.push(nod_start);
    while(!coada.empty())
    {
        fr = coada.top();
        coada.pop();
        marime = graphs[fr].size();
        checked[fr] = true;
        for(contor = 0; contor < marime; contor++)
            if(distante[fr] + graphs[fr][contor].cost < distante[graphs[fr][contor].x])
            {
                distante[graphs[fr][contor].x] = distante[fr] + graphs[fr][contor].cost;
                if(!checked[graphs[fr][contor].x])
                    coada.push(graphs[fr][contor].x), checked[graphs[fr][contor].x] = 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;
}