Cod sursa(job #2174804)

Utilizator Victor24Vasiesiu Victor Victor24 Data 16 martie 2018 13:33:54
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;

int n, m, a, b, c, viz[50005], cst[50005];

ifstream f ("dijkstra.in");
ofstream g ("dijkstra.out");

vector < pair < int, int > > G[50005];

void dijkstra()
{
    priority_queue < pair < int, int >, vector < pair < int, int > > , greater < pair < int, int> > > pq;

    pair < int, int > aux;


    pq.push( { 0, 1 } );
    viz[1] = 1;

    while ( !pq.empty() )
    {
        aux = pq.top();
        pq.pop();

        for ( auto i : G[aux.y] )
        {
            if ( viz [i.x] == 0 )
            {
                pq.push ( { aux.x + i.y  , i.x } );
                viz[ i.x ] = 1;
                cst[ i.x ] = aux.x + i.y;
            }
        }

    }

}

int main ()
{
    f>>n>>m;

    for ( int i = 1 ; i <= m ; i++ )
    {
        f>>a>>b>>c;

        G[a].push_back( {b, c} );
    }

    dijkstra();

    for ( int i = 2 ; i <= n ; i++ )
    {
        g<<cst[i]<<" ";
    }

    return 0;
}