Bellman Ford Algorithm

The Bellman Ford Algorithm is a graph-based algorithm designed to find the shortest path between a given source node and all other nodes in a weighted graph. This algorithm is particularly useful in scenarios where the graph may contain negative weight edges and can also detect negative weight cycles. The fundamental idea behind the Bellman Ford Algorithm is to progressively relax the edges, iteratively updating the shortest path estimates until convergence is reached. This is achieved by performing a series of relaxation steps, where each step consists of looping through all the edges in the graph and updating the distance values of the connected nodes if a shorter path is found. The algorithm starts by initializing the distance of the source node to zero and the distance of all other nodes to infinity. It then iteratively relaxes all the edges in the graph for a total of |V|-1 times, where |V| is the number of vertices in the graph. In each relaxation step, the algorithm checks if the distance from the source node to the adjacent node through the current edge is shorter than the previously stored distance value. If a shorter path is found, the distance value is updated accordingly. After completing the relaxation steps, one final iteration is performed to detect any negative weight cycles. If a shorter path is found during this iteration, it implies the presence of a negative weight cycle, and the algorithm reports the cycle. Otherwise, the final distance values represent the shortest path from the source node to all other nodes in the graph.
/**************************************************************************************

    Bellman-Ford algorithm finding shortest distances from a single vertex
    in graph. Works in O(N*M)

    Based on problem 178 from informatics.mccme.ru
    http://informatics.mccme.ru/mod/statements/view3.php?id=260&chapterid=178#1

**************************************************************************************/

#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cassert>
#include <utility>
#include <iomanip>

using namespace std;

const int MAXN = 105;
const int INF = 30000;

struct edge {
    int from, to;
    int w;
};

int n, m;
int dist[MAXN];
vector <edge> e;

int main() {
    //assert(freopen("input.txt","r",stdin));
    //assert(freopen("output.txt","w",stdout));

    scanf("%d %d", &n, &m);

    for (int i = 1; i <= m; i++) {
        edge curEdge;
        scanf("%d %d %d", &curEdge.from, &curEdge.to, &curEdge.w);
        e.push_back(curEdge);
    }

    for (int i = 1; i <= n; i++)
        dist[i] = INF;
    dist[1] = 0;

    for (int i = 1; i <= n; i++) {
        bool changed = false;
        for (int j = 0; j < m; j++) {
            int from = e[j].from, to = e[j].to, w = e[j].w;
            if (dist[from] != INF && dist[from] + w < dist[to]) {
                dist[to] = dist[from] + w;
                changed = true;
            }
        }
        if (!changed)
            break;
    }

    for (int i = 1; i <= n; i++)
        printf("%d ", dist[i]);

    return 0;
}

LANGUAGE:

DARK MODE: