Cod sursa(job #1957880)

Utilizator vlasiuflaviusVlasiu Flavius vlasiuflavius Data 7 aprilie 2017 20:30:52
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
#include <vector>
#include <queue>
#define x first
#define y second
using namespace std;
ofstream fout ("dijkstra.out");
ifstream fin ("dijkstra.in");
vector < pair < int , int > > v[50005];
priority_queue < pair < long long , int > > q;
pair < int , int > aux;
const long long oo = 2e14;
int n,m,i,a,b,c;
long long rsp[50005];
int use[50005];
int main()
{
    fin>>n>>m;
    for( i = 1 ; i <= m ; i++ )
    {
        fin>>a>>b>>c;
        v[ a ].push_back( make_pair( b , c ) );
    }
    for( i = 1 ; i <= n ; i++ )
        rsp[ i ] = oo;
    q.push( make_pair( 0 , 1 ) );
    while( q.size() )
    {
        aux = q.top();
        q.pop();
        if( use[ aux.y ] )
            continue;
        aux.x *= -1;
        use[ aux.y ] = 1;
        rsp[ aux.y ] = aux.x;
        for( auto it : v[ aux.y ] )
            if( !use[ it.y ] )
                q.push( make_pair( -( aux.x + it.y ) , it.x ) );
    }
    for( i = 2 ; i <= n ; i++ )
        fout<< ( rsp[ i ] != oo ? rsp[ i ] : 0 )<<" ";
}