Cod sursa(job #3271860)

Utilizator cezarinfoTulceanu Cezar cezarinfo Data 27 ianuarie 2025 16:21:59
Problema Algoritmul Bellman-Ford Scor 55
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include<iostream>
#include<fstream>
#include<vector>
#include<cstring>
using namespace std;
ifstream in("bellmanford.in");
ofstream out("bellmanford.out");
const int NMAX=50005;
const int MMAX=250007;
const int INF=1000000009;
int n,m,a,b,c,i;
int cost[NMAX];
bool change=0;
bool good;
struct ed
{
    int x,y,w;
};
ed aux;
vector<ed> edj;
int main()
{

    in>>n>>m;
    for(i=1;i<=m;i++)
    {
        in>>a>>b>>c;
        aux.x=a;
        aux.y=b;
        aux.w=c;
        edj.push_back(aux);
    }
    memset(cost,INF,sizeof(cost));
    cost[1]=0;
    good=0;
    for(i=1;i<=n/2;i++)
    {
        change=0;
        for(auto g:edj)
        {
            if(cost[g.x]+g.w<cost[g.y])
            {
                change=1;
                cost[g.y]=cost[g.x]+g.w;
            }
        }
        if(change==0)
        {
            good=1;
            break;
        }
    }
    if(good==0)
    {
        out<<"Ciclu negativ!";
    }
    else
    {
        for(i=2;i<=n;i++)
        {
            out<<cost[i]<<" ";
        }
    }
}