Pagini recente » Cod sursa (job #2946968) | Cod sursa (job #2322129) | Cod sursa (job #310139) | Cod sursa (job #1621888) | Cod sursa (job #1732587)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ofstream fout ("dijkstra.out");
ifstream fin ("dijkstra.in");
vector < pair < int , int > > G[ 50010 ];
queue <int> q;
int a,b,c,n,m,cost[ 50050 ],i,aux;
const int oo = 2000000000;
int main()
{
fin>>n>>m;
for( i = 1 ; i <= m ; i++ )
{
fin>>a>>b>>c;
G[ a ].push_back( make_pair( b , c ) );
}
for( i = 2 ; i <= n ; i++ )
cost[ i ] = oo;
cost[ 1 ] = 0;
q.push( 1 );
while( !q.empty() )
{
aux = q.front();
q.pop();
for( i = 0 ; i < G[ aux ].size() ; i++ )
{
if( cost[ G[ aux ][ i ].first ] == oo || cost[ G[ aux ][ i ].first ] > cost[ aux ] + G[ aux ][ i ].second )
{
cost[ G[ aux ][ i ].first ] = cost[ aux ] + G[ aux ][ i ].second;
q.push( G[ aux ][ i ].first );
}
}
}
for( i = 2 ; i <= n ; i++ )
if( cost[ i ] == oo)
cost[ i ] = 0;
for( i = 2 ; i <= n ; i++ )
fout<<cost[ i ]<<" ";
return 0;
}