Pagini recente » Cod sursa (job #2429230) | Cod sursa (job #2592265) | Borderou de evaluare (job #2017054) | Cod sursa (job #929579) | Cod sursa (job #435756)
Cod sursa(job #435756)
/*
* File: main.cpp
* Author: VirtualDemon
*
* Created on April 7, 2010, 8:38 PM
*/
#include <queue>
#include <cstdlib>
#include <fstream>
#include <iterator>
/*
*
*/
using namespace std;
typedef pair< int, int > pr;
vector< int > d;
vector< bool > inH, was;
vector< vector< pr > > G;
vector< pr >::const_iterator it, iend;
class cmp
{
public :
inline bool operator() ( const int& x, const int& y ) const
{
return d[x] > d[y];
}
};
priority_queue< int, vector<int>, cmp > H;
int main( void )
{
int N, M, x, y, c;
ifstream in( "dijkstra.in" );
in>>N>>M;
G.resize(N);
for( ; M; --M )
{
in>>x>>y>>c;
--x, --y;
G[x].push_back( pr( y, c ) );
}
d.resize(N);
inH.resize(N);
was.resize(N);
for( H.push(0); !H.empty(); H.pop() )
{
x=H.top();
inH[x]=false;
for( it=G[x].begin(), iend=G[x].end(); it < iend; ++it )
{
y=it->first, c=it->second;
if( !was[y] )
{
d[y]=d[x]+c;
H.push(y);
inH[y]=was[y]=true;
continue;
}
if( d[y] > d[x]+c )
{
d[y]=d[x]+c;
if( !inH[y] )
{
H.push(y);
inH[y]=true;
}
}
}
}
ofstream out( "dijkstra.out" );
copy( d.begin()+1, d.end(), ostream_iterator<int>( out, " " ) );
out<<'\n';
return EXIT_SUCCESS;
}