Cod sursa(job #459938)

Utilizator BitOneSAlexandru BitOne Data 31 mai 2010 17:31:11
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#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;
}