Cod sursa(job #2262446)

Utilizator slym777Darii Dan slym777 Data 17 octombrie 2018 12:33:24
Problema Algoritmul Bellman-Ford Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <iostream>
#include <bits/stdc++.h>
#define inf 200000
using namespace std;

ifstream fin("bellman.in");
ofstream fout("bellman.out");

struct muchie{
    int x,y,c;
};

int tata[260000];
int dist[260000];

vector < muchie > V;
int main()
{
    int n,m;
    bool flag = true;
    fin >> n >> m;
    V.resize(m);
    for (auto &it : V)
        fin >> it.x >> it.y >> it.c;
    for (int i = 1 ; i <= n; i++)
    {
        tata[i] = 0;
        dist[i] = inf;
    }
    dist[1] = 0;
    for (int i = 1 ; i <= n; i++)
        for (auto &it : V)
        {
            if (dist[it.x] + it.c < dist[it.y])
                dist[it.y] = dist[it.x] + it.c;
            tata[it.y] = it.x;
        }
    for (auto &it : V)
        if (dist[it.x] + it.c < dist[it.y] && flag)
            {
                fout << "Ciclu negativ!";
                flag = false;
            }
    if (flag)
        for (int i = 2 ; i <= n; i++)
            fout << dist[i] << " ";

    return 0;
}