Cod sursa(job #2392760)

Utilizator Andrei-27Arhire Andrei Andrei-27 Data 30 martie 2019 13:08:43
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
using namespace std ;
const int NR =  50005 , oo = ( 1 << 30 ) ;
ifstream in ("dijkstra.in") ;
ofstream out ("dijkstra.out") ;
int n , m , x , y , c ;
vector < pair < int , int > > v [ NR ] ;
vector < int > d ( NR , oo ) ;
struct cmp  {
inline bool operator() ( const pair < int , int > i , const pair < int , int > j )    {
return i.s > j.s ;
}
};
priority_queue < pair < int , int > , vector < pair < int , int > > , cmp > q ;
static void dijkstra ( )  {
    int nod , cost ;
    d [ 1 ] = 0 ;
    q.push( { 1 , 0 } ) ;
    while ( !q.empty() )    {
        nod = q.top().f ;
        cost = q.top().s ;
        q.pop() ;
        if ( cost != d [ nod ] )    continue ;

        for ( vector < pair < int , int > > :: iterator it = v [ nod ].begin() ; it < v [ nod ].end() ; ++ it ) {
            if ( d [ nod ] + (*it).s < d [ (*it).f ] )  {
                d [ (*it).f ] = d [ nod ] + (*it).s ;
                 q.push( { (*it).f , d [ (*it).f ] } ) ;
            }
        }
    }
    for ( nod = 2 ; nod <= n ; ++ nod )
        if ( d [ nod ] == oo )  out << "0 " ;
        else                    out << d [ nod ] << ' ' ;
}
int main () {
    in >> n >> m ;
    while ( m -- )  {
        in >> x >> y >> c ;
        v [ x ].pb ( { y , c } ) ;
    }
    dijkstra ( ) ;
}