Cod sursa(job #2955497)

Utilizator xDemonstyMatei Haba Ionut xDemonsty Data 17 decembrie 2022 11:24:37
Problema Algoritmul Bellman-Ford Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#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;
}