Pagini recente » Cod sursa (job #67317) | Romanii medaliati la IOI | Cod sursa (job #279803) | Cod sursa (job #606644) | Cod sursa (job #446902)
Cod sursa(job #446902)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on April 26, 2010, 8:41 PM
*/
#include <queue>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 50011
#define oo 0x3f3f3f3f
/*
*
*/
using namespace std;
typedef pair< size_t, size_t > pr;
bool inH[Nmax];
size_t d[Nmax];
vector< pr > G[Nmax];
vector< pr >::const_iterator it, iend;
class cmp
{
public :
inline bool operator() ( const size_t& x, const size_t& y ) const
{
return d[x] > d[y];
}
};
priority_queue< size_t, vector< size_t >, cmp > PQ;
int main(int argc, char** argv)
{
int N, M, x, y, c;
ifstream in( "dijkstra.in" );
for( in>>N>>M; M; --M )
{
in>>x>>y>>c;
G[x].push_back( pr( y, c ) );
}
fill( d+2, d+N+1, oo );
for( PQ.push(1); !PQ.empty(); PQ.pop() )
{
x=PQ.top();
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] )
{
PQ.push(y);
inH[y]=true;
}
}
}
}
ofstream out( "dijkstra.out" );
for( x=2; x <= N; ++x )
if( oo == d[x] )
out<<"0 ";
else out<<d[x]<<' ';
out<<'\n';
return (EXIT_SUCCESS);
}