Cod sursa(job #2622198)

Utilizator etienAndrone Stefan etien Data 31 mai 2020 17:44:25
Problema Algoritmul Bellman-Ford Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include<fstream>
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
int n,m,i,j,d[50001];
const int INF=2e9;
struct muchie
{
    int x,y,cost;
};
muchie v[250001];
int main()
{
    fin>>n>>m;
    fill(d+2,d+n+1,INF);
    for(i=1;i<=m;i++)
    {
        fin>>v[i].x>>v[i].y>>v[i].cost;
    }
    bool ok;
    for(i=1;i<n;i++)
    {
        ok=true;
        for(j=1;j<=m;j++)
        {
            if(d[v[j].x]!=INF)
                if(d[v[j].x]+v[j].cost<d[v[j].y])
                {
                    d[v[j].y]=d[v[j].x]+v[j].cost;
                    ok=false;
                }
        }
        if(ok)
            break;
    }
    for(j=1;j<=m;j++)
    {
        if(d[v[j].x]!=INF)
            if(d[v[j].x]+v[j].cost<d[v[j].y])
            {
                fout<<"Ciclu negativ!";
                return 0;
            }
    }
    for(i=2;i<=n;i++)
        fout<<d[i]<<" ";

}