Cod sursa(job #2209393)

Utilizator ElektrykT E S L A P E F E L I E Elektryk Data 3 iunie 2018 11:36:00
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <fstream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdlib>
#define INFINITE 1e9

using namespace std;

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

int n, m, checked[50001], path[50001], x, y, c;
vector < pair <int , int> > v[50001];
queue <int> q;
void BellmanFord(int nod)
{
    path[nod]=0;
    q.push(nod);
    while(!q.empty())
    {
        nod=q.front();
        q.pop();
        checked[nod]++;
        if(checked[nod]>n)
        {
            out<<"Ciclu negativ!";
            exit(0);
        }
        for(register int i=0; i<v[nod].size(); ++i)
        {
            if( path[nod]+v[nod][i].second < path[v[nod][i].first] )
            {
                path[v[nod][i].first] = path[nod]+v[nod][i].second;
                q.push(v[nod][i].first);
            }
        }
    }
}

int main()
{
    in>>n>>m;
    for(register int i=1; i<=m; ++i)
    {
        in>>x>>y>>c;
        v[x].push_back( { y , c } );
    }
    fill(path+1, path+n+1, INFINITE);
    BellmanFord(1);
    for(register int i=2; i<=n; ++i)
        out<<path[i]<<" ";
    return 0;
}