Pagini recente » Cod sursa (job #2384293) | Cod sursa (job #1745667) | Cod sursa (job #2970135) | Cod sursa (job #1552682) | Cod sursa (job #2392760)
#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 ( ) ;
}