Pagini recente » Cod sursa (job #2349693) | Cod sursa (job #1886134) | Cod sursa (job #1885402) | Cod sursa (job #2894002) | Cod sursa (job #2955497)
#include <fstream>
#include <queue>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
queue <int> q ;
int t [ 101 ] [ 101 ] ;
int cost [ 101 ] ;
int main()
{
int n , m ;
in >> n >> m ;
queue <int> q ;
int maxim = 500001 ;
int x , y , c ;
cost [ m ] = 0 ;
while ( in >> x >> y >> c)
t [ x ] [ y ] = c ;
for ( int i = 1 ; i <= n ; i ++)
{
if ( i != m )
cost [ i ] = maxim ;
}
q.push ( m ) ;
while ( !q.empty() )
{
int x = q .front();
for ( int i = 1 ; i <= n ; i ++ )
{
if ( t [ x ] [ i ] != 0 )
{
if ( t [x ] [ i ] + cost [ x ] < cost [ i ])
{
q.push ( i ) ;
cost [ i ] = t [ x ] [ i ] + cost [ x ] ;
}
}
}
q.pop() ;
}
for ( int i = 1 ; i <= n ; i ++)
{
if ( cost [ i ] == maxim )
out << -1 << " ";
else
out << cost [ i ] << " ";
}
return 0;
}