Pagini recente » Runda 2 preONI 2007 | Cod sursa (job #3232602) | Cod sursa (job #485474) | Cod sursa (job #3187012) | Cod sursa (job #479182)
Cod sursa(job #479182)
/*
* File: main.cpp
* Author: bitone
*
* Created on August 23, 2010, 10:47 AM
*/
#include <queue>
#include <limits>
#include <fstream>
#include <cstdlib>
#include <iterator>
#include <algorithm>
#define MAX_N 50011
#define oo std::numeric_limits< signed int >::max()/2
using namespace std;
/*
*
*/
typedef signed int sint;
typedef pair< sint, sint > spair;
sint d[MAX_N];
bool InPq[MAX_N];//, was[MAX_N];
vector< spair > G[MAX_N];
vector< spair >::const_iterator it, iend;
class cmp
{
public : bool operator() ( const sint& x, const sint& y ) const
{
return d[x] > d[y];
}
};
priority_queue< sint, vector< sint >, cmp > Pq;
int main(int argc, char** argv)
{
sint N, M, c, x, y;
ifstream in( "dijikstra.in" );
for( in>>N>>M; M; --M )
{
in>>x>>y>>c;
G[x].push_back( spair( y, c ) );
}
fill( d+2, d+N+1, oo );
for( Pq.push(1); !Pq.empty(); Pq.pop() )
{
x=Pq.top();
InPq[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;
// was[it->first]=true;
if( !InPq[it->first] )
{
Pq.push(it->first);
InPq[it->first]=true;
}
}
}
replace( d+2, d+N+1, oo, 0 );
ofstream out( "dijikstra.out" );
copy( d+2, d+N+1, ostream_iterator<sint>( out, " ") );
out<<'\n';
return EXIT_SUCCESS;
}