Pagini recente » Cod sursa (job #3271710) | Cod sursa (job #3289646) | Cod sursa (job #2882609) | Cod sursa (job #30939) | Cod sursa (job #459938)
Cod sursa(job #459938)
#include <queue>
#include <cstdlib>
#include <fstream>
#include <iterator>
#define Nmax 50111
#define oo 9999999
/*
*
*/
using namespace std;
typedef pair< int, int > pr;
int d[Nmax];
bool isPq[Nmax];
vector< pr > G[Nmax];
vector< pr >::const_iterator it, iend;
class cmp
{
public:
inline bool operator() ( const int& x, const int& y )
{
return d[x] > d[y];
}
};
priority_queue< int, vector< int >, cmp > pQ;
int main( void )
{
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();
isPq[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( false == isPq[y] )
{
pQ.push(y);
isPq[y]=true;
}
}
}
}
ofstream out( "dijkstra.out" );
copy( d+2, d+N+1, ostream_iterator<int>( out, " " ) );
out<<'\n';
return EXIT_SUCCESS;
}