Cod sursa(job #2659904)

Utilizator Snake2003lalallalal Snake2003 Data 17 octombrie 2020 19:15:39
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

#define Nmax 50005

using namespace std;

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

vector < pair < int, int > > Numbers[Nmax];

priority_queue < int, vector < int >, greater < int > > Coada;

int Distanta[Nmax];

int vf, muchii;

void Dijkstra( int x )
{
    int Nod, Vecin, Cost;

    Coada.push(x);
    Distanta[x] = 0;

    while( !Coada.empty() )
    {
        Nod = Coada.top();
        Coada.pop();
        for( unsigned int i = 0; i < Numbers[Nod].size(); i ++ )
        {
            Vecin = Numbers[Nod][i].first;
            Cost = Numbers[Nod][i].second;

            if( Distanta[Nod] + Cost < Distanta[Vecin] )
            {
                Distanta[Vecin] = Distanta[Nod] + Cost;
                Coada.push(Vecin);
            }
        }
    }

}

int main()
{
    fin >> vf >> muchii;
    int x, y, cost;
    while( fin >> x >> y >> cost )
     {
        Numbers[x].push_back({ y, cost });
     }

    for( int i = 2; i<= vf; ++ i )
            Distanta[i] = 1e9;

    Dijkstra(1);

    for( int i = 2; i <= vf; ++ i )
        if( Distanta[i] == 1e9 )
            fout << -1 << " ";
        else
            fout << Distanta[i] << " ";

    return 0;
}