Cod sursa(job #2686102)

Utilizator speedypleathGheorghe Andrei speedypleath Data 18 decembrie 2020 15:26:23
Problema Algoritmul Bellman-Ford Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("bellmanford.in");
ofstream out("bellmanford.out");
const int INF = 1000000;
struct edge
{
    int from,to,cost;
};
vector<edge> edges;
int n,m,dist[100005],node;

int main()
{
    in>>n>>m;
    for(int i=0;i<m;i++)
    {
        int x,y,z;
        edge e;
        in>>x>>y>>z;
        e.from = x-1;
        e.to = y-1;
        e.cost = z;
        edges.push_back(e);
    }
    node = 0;
    for(int i=0;i<n;i++)
        dist[i] = INF;

    dist[node] = 0;
    for(int i=0;i<n;i++)
        for(auto edge:edges)
            if(dist[edge.to]>dist[edge.from]+edge.cost)
                dist[edge.to] = dist[edge.from]+edge.cost;

    if(dist[0] != 0)
        for(int i=1;i<n;i++)
            out<<dist[i]<<' ';
    else
        out<<"Ciclu negativ!";

    return 0;
}