Cod sursa(job #468414)

Utilizator R.A.RFMI Romila Remus Arthur R.A.R Data 3 iulie 2010 14:33:46
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <queue>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <algorithm>
#define Nmax 50111
#define oo 9999999
using namespace std;

ifstream in("dijkstra.in");
ofstream out("dijkstra.out");

typedef pair< int, int > pr;
long d[Nmax];
vector< pair<int,int> > G[Nmax];
vector< pair<int,int> >::iterator it,iend;
bool isPq[Nmax];

class cmp{
public :
    inline bool operator()(int x,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;
                }
            }
        }
    }
    replace( d+2, d+N+1, oo, 0 );
    ofstream out( "dijkstra.out" );
    copy( d+2, d+N+1, ostream_iterator<int>( out, " " ) );
    out<<'\n';
    return EXIT_SUCCESS;
}

//http://infoarena.ro/job_detail/459940?action=view-source