Pagini recente » Cod sursa (job #646006) | Cod sursa (job #2076675) | Cod sursa (job #2023993) | Cod sursa (job #293806) | Cod sursa (job #1702487)
/**
* Code by Patrick Sava
* "Spiru Haret" National College of Bucharest
**/
#include <bits/stdc++.h>
using namespace std;
ifstream fin ( "dijkstra.in" ) ;
ofstream fout ( "dijkstra.out" ) ;
const int MAX = 50014 ;
struct cmp {
bool operator ( ) ( const pair < int , int > &a , const pair < int , int > &b ) const
{
return a.second > b.second ;
}
};
priority_queue < pair < int , int > , vector < pair < int , int > > , cmp > H ;
vector < pair < int , int > > gr [ MAX ] ;
int dist [ MAX ] ;
int main()
{
int n , m ;
fin >> n >> m ;
for ( int i = 1 ; i <= m ; ++ i ) {
int x , y , cost ;
fin >> x >> y >> cost ;
gr [ x ].push_back ( make_pair ( y , cost ) ) ;
}
for ( int i = 1 ; i <= n ; ++ i ) {
dist [ i ] = 1 << 30 ;
}
dist [ 1 ] = 0 ;
H.push ( make_pair ( 1 , dist [ 1 ] ) ) ;
while ( !H.empty() )
{
int nod = H.top().first ;
int cost = H.top().second ;
H.pop() ;
if ( dist [ nod ] != cost ) {
continue ;
}
for ( auto x : gr [ nod ] ) {
if ( dist [ x.first ] > dist [ nod ] + x.second ) {
dist [ x.first ] = dist [ nod ] + x.second ;
H.push ( make_pair ( x.first , dist [ x.first ] ) ) ;
}
}
}
for ( int i = 2 ; i <= n ; ++ i ) {
if ( dist [ i ] == 1 << 30 ) {
fout << 0 << ' ' ;
}
else {
fout << dist [ i ] << ' ' ;
}
}
return 0;
}