Cod sursa(job #2574669)

Utilizator tudorrrTudor Moldovan tudorrr Data 6 martie 2020 08:28:31
Problema Algoritmul Bellman-Ford Scor 15
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include<fstream>
#include<vector>
#include<set>
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
set<pair<int,pair<int,int>>>G;
const int INF = 0x3f3f3f3f;
int D[50005];
int n,m;
void BF(int x)
{

}
int main()
{
    fin>>n>>m;
    for(int i=1;i<=n;i++)
        D[i]=INF;
    for(int i=1;i<=m;i++)
    {
        int x,y,w;
        fin>>x>>y>>w;
        G.insert({w,{x,y}});
    }
    D[1]=0;
    BF(1);
    for(int i=1;i<=n;i++)
    {
        for(auto p : G)
        {
            int w=p.first;
            auto p2=p.second;
            int x=p2.first;
            int y=p2.second;
            if(D[y]>D[x]+w)
                D[y]=D[x]+w;
        }
    }
    bool pp=false;
    for(auto p : G)
    {
        int w=p.first;
        auto p2=p.second;
        int x=p2.first;
        int y=p2.second;
        if(D[y]>D[x]+w)
        {
            pp=true;
            break;
        }
    }
    if(pp)
    {
        fout<<"Ciclu negativ";
    }
    else
    {
        for(int i=2;i<=n;i++)
            fout<<D[i]<<" ";
    }
    return 0;
}