Cod sursa(job #479182)

Utilizator BitOneSAlexandru BitOne Data 23 august 2010 11:13:50
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
/* 
 * 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;
}