Cod sursa(job #2223248)

Utilizator cezarzbughinCezar Zbughin cezarzbughin Data 19 iulie 2018 15:29:56
Problema Algoritmul Bellman-Ford Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("bellmanford.in");
ofstream g("bellmanford.out");
const int N = 50010;
const int oo = 1000000007;
int n,m,x,y,c,d[N];
queue<int> Q;
vector<tuple<int,int,bool>> v[N];
bool p;
int main()
{
    f>>n>>m;
    for(;m;m--)
    {
        f>>x>>y>>c;
        v[x].push_back(make_tuple(y,c,false));
    }
    fill(d+2,d+n+1,oo);
    Q.push(1);
    while(Q.size())
    {
        x=Q.front();
        for(auto &it:v[x])
        {
            tie(y,c,p)=it;
            if(d[y]>d[x]+c)
            {
                if(p)
                {
                    g<<"Ciclu negativ!";
                    return 0;
                }
                it=make_tuple(y,c,true);
                d[y]=d[x]+c;
                Q.push(y);
            }
        }
        Q.pop();
    }
    for(int i=2;i<=n;i++)
        g<<d[i]<<' ';
    return 0;
}