Pagini recente » Cod sursa (job #442637) | Borderou de evaluare (job #804598) | Cod sursa (job #3226571) | Cod sursa (job #333729) | Cod sursa (job #492176)
Cod sursa(job #492176)
/*
* File: main.cpp
* Author: bitone
*
* Created on October 13, 2010, 5:33 PM
*/
#include <queue>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define MAX_N 50011
#define oo 1000000000
using namespace std;
typedef pair< int, int > pr;
/*
*
*/
int d[MAX_N];
bool was[MAX_N];
vector< pr > G[MAX_N];
vector< pr >::const_iterator it, iend;
class Compare
{
public :
inline bool operator() ( const int& x, const int& y ) const
{
return d[x] > d[y];
}
};
priority_queue< int, vector<int>, Compare > 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();
was[x]=false;
for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
if( d[it->first] > d[x]+it->second )
{
d[it->first]=d[x]+it->second;
if( false == was[it->first] )
{
pQ.push(it->first);
was[it->first]=true;
}
}
}
ofstream out( "dijkstra.out" );
copy( d+2, d+N+1, ostream_iterator<int>( out, " " ) );
out<<'\n';
return EXIT_SUCCESS;
}