Cod sursa(job #2636824)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 20 iulie 2020 11:57:03
Problema Algoritmul Bellman-Ford Scor 35
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

const int inf = 1000000000;

int n, m;
vector<pair<pair<int, int>, int> > muchii;
int dist[50005];

void citire() {
    fin >> n >> m;
    while(m--) {
        int x, y, c;
        fin >> x >> y >> c;
        muchii.push_back({{x, y}, c});
    }
}

void solve() {
    for(int i = 1; i<= n; i++)
        dist[i] = inf;
    dist[1] = 0;

    for(int i = 1; i < n; i++)
        for(auto x: muchii)
            if(dist[x.first.first]+x.second < dist[x.first.second])
                dist[x.first.second] = dist[x.first.first]+x.second;

    for(auto x: muchii)
        if(dist[x.first.first]+x.second < dist[x.first.second]) {
            fout << "Ciclu negativ!";
            return;
        }

    for(int i = 2; i<= n; i++)
        fout << dist[i] << ' ';
}

int main() {
    citire();
    solve();
}