Advertisement
oaktree

bellman-ford.cpp (rough)

Jun 5th, 2016
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. /*
  2.     Bellman-Ford with an Adjacency List
  3. */
  4. #include <vector>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <limits>
  8. #include <utility>
  9.  
  10. typedef std::vector< std::vector< std::pair<int,int> > > adj_list;
  11.  
  12. void bellman(const int start, const adj_list& graph, std::vector<int>& distances);
  13.  
  14. int main() {
  15.     int n,m; std::cin >> n >> m;
  16.    
  17.     // adj-list using vector of size n holding a vector for
  18.     // each vertex.
  19.     adj_list graph(n);
  20.    
  21.     int f,s,w; // first, second, weight
  22.     for (int i = 0; i < m; i++) {
  23.         std::cin >> f >> s >> w;      
  24.         graph[f-1].push_back( std::make_pair(s-1,w) );
  25.     }
  26.    
  27.     int start; std::cin >> start;
  28.    
  29.     std::vector<int> distances(n /* n = graph.size() */, std::numeric_limits<int>::max());
  30.     bellman(start - 1, graph, distances);
  31.  
  32.     // print out the distances here
  33.     for (int i = 0; i < n; i++)
  34.         std::cout << "The distance from " << start << " to "
  35.             << i+1 << " is " << distances[i] << ".\n";
  36.  
  37.     return 0;
  38. }
  39.  
  40. void bellman(const int start, const adj_list& graph, std::vector<int>& distances) {
  41.     // setting to infinity is done in main;
  42.     distances[start] = 0;
  43.  
  44.     // for later.
  45.     std::vector<int> pred(graph.size());
  46.  
  47.     // visit every node
  48.     // start with `start`
  49.     // keep track of predecessors
  50.  
  51.     // do it V-1 times
  52.     bool changes_made = true;
  53.     for (int i = 1, n = graph.size(); i < n; i++) {
  54.         // go through each node
  55.         for (int j = 0; j < n; j++) {
  56.             if (distances[j] == std::numeric_limits<int>::max())
  57.                 continue; // skip if we don't know how to reach a node yet.
  58.            
  59.             // go through a node's neighbors
  60.             for (auto& neighbor : graph[j]) {
  61.                 if (distances[neighbor.first] > distances[j] + neighbor.second) {
  62.                     distances[neighbor.first] = distances[j] + neighbor.second;
  63.                     pred[neighbor.first] = j;
  64.                     changes_made = true;
  65.                 }
  66.             }
  67.         }
  68.         if (!changes_made)
  69.             break;
  70.         changes_made = false;
  71.     }
  72.  
  73.     // now we do it one more time to find any negative cycles
  74.     for (int i = 0, n = graph.size(); i < n; i++) {
  75.         if (distances[i] == std::numeric_limits<int>::max())
  76.             continue; // skip if we don't know how to reach a node yet.
  77.        
  78.         // go through a node's neighbors
  79.         for (auto& neighbor : graph[i]) {
  80.             if (distances[neighbor.first] > distances[i] + neighbor.second)
  81.                 std::cout << "Found negative cycle from " << i << " to " << neighbor.first << ".\n";
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement