Cod sursa(job #435021)
/*
* File: main.cpp
* Author: SpeeDemon
*
* Created on April 6, 2010, 7:35 PM
*/
#include <queue>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define oo 0x3f3f3f3f
/*
*
*/
using namespace std;
typedef pair< int, int > pr;
vector< int > d;
vector< bool > inH;
vector< vector< pr > > G;
vector< pr >::const_iterator it, iend;
class cmp
{
public:
inline bool operator() ( const int& x, const int& y ) const
{
return d[x] > d[y];
}
};
priority_queue< int, vector<int>, cmp > H;
int main( void )
{
int N, M, x, y, c;
ifstream in( "dijkstra.in" );
in>>N>>M;
G.resize(N);
for( ; M; --M )
{
in>>x>>y>>c;
--x, --y;
G[x].push_back( pr( y, c ) );
}
d.resize( N );
inH.resize( N );
d.assign( N, oo );
for( d[0]=0, H.push(0); !H.empty(); )
{
x=H.top(); H.pop();
inH[x]=false;
for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
{
y=it->first; c=it->second;
if( d[y] > d[x]+c )
{
d[y]=d[x]+c;
if( !inH[y] )
{
H.push(y);
inH[y]=true;
}
}
}
}
ofstream out( "dijkstra.out" );
for( x=1; x < N; ++x )
{
if( oo == d[x] )
out<<'0';
else out<<d[x];
out<<' ';
}
out<<'\n';
return EXIT_SUCCESS;
}