Fastrail08

Bellman Ford Algorithm

Aug 24th, 2025 (edited)
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. /*
  2. To make sure you catch any kind of bug in raised in your own environment, compile with:
  3. g++ -Wall -Wextra -Werror -std=c++17 your_code.cpp
  4.  
  5. -Wall: Turn on most warnings
  6.  
  7. -Wextra: Turn on extra warnings
  8.  
  9. -Werror: Treat warnings as errors (forces you to fix them!)
  10.  
  11. -std=c++17: Or whatever version you're using
  12. */
  13. #include <bits/stdc++.h>
  14. using namespace std;
  15.  
  16. vector<int> bellmanFord(int V, vector<vector<int> > &edges, int src){
  17.     vector<int> distance(V, INT_MAX);
  18.    
  19.     //Init src distance = 0
  20.     distance[src] = 0;
  21.    
  22.     // Relax Edges V - 1 times
  23.     for(int k = 0; k < V - 1; k++){
  24.         for(int i = 0; i < edges.size(); i++){
  25.             int u = edges[i][0], v = edges[i][1], w = edges[i][2];
  26.            
  27.             //If distance of u is INT_MAX, then relaxation can't happen, can't reduce the distance
  28.             //dist[v] = dist[u] + w and dist[u] = INT_MAX
  29.             if(distance[u] != INT_MAX){
  30.                 //Relax edge
  31.                 if(distance[u] + w < distance[v]){
  32.                     distance[v] = distance[u] + w;
  33.                 }
  34.             }
  35.         }
  36.     }
  37.    
  38.     // Try relaxing the edges V - 1 times more, if any changes occur in the distance array
  39.     // Changes means: Negative self loop or Negative weighted cycle
  40.     //Nodes affected: (DIRECTYLY) Nodes directly involved in cycle OR (INDIRECTLY) the nodes which are reachable by the cycle nodes but not directly a part of a cycle
  41.     bool negativeCycle = false;
  42.     for(int k = 0; k < V; k++){
  43.         for(int i = 0; i < edges.size(); i++){
  44.             int u = edges[i][0], v = edges[i][1], w = edges[i][2];
  45.             if(distance[u] != INT_MAX){
  46.                 //Nodes getting relaxed in the 2nd run of algorithm are CONTAMINATED by cycle
  47.                 if(distance[u] + w < distance[v]){
  48.                     negativeCycle = true;
  49.                     distance[v] = INT_MAX;
  50.                 }
  51.             }
  52.         }
  53.     }
  54.    
  55.     if(negativeCycle){
  56.         vector<int> res = {-1};
  57.         return res;
  58.     }
  59.     else{
  60.         return distance;
  61.     }
  62. }
  63.  
  64. int main() {
  65.     // your code goes here
  66.     int V, e, src;
  67.     cin >> V >> e;
  68.     vector<vector<int> > edges(e, vector<int>(3));
  69.    
  70.     for(int i = 0; i < e; i++){
  71.         cin >> edges[i][0] >> edges[i][1] >> edges[i][2];
  72.     }    
  73.     cin >> src;
  74.     vector<int> res = bellmanFord(V, edges, src);
  75.     for(int i : res){
  76.         cout << i << " ";
  77.     }
  78.    
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment