Advertisement
Guest User

Untitled

a guest
May 27th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. const long long INF = 1e18;
  2.  
  3. struct Edge {
  4.     int x, y;
  5.     int cst;
  6. };
  7.  
  8. inline long long BellmanFord(int n, int m, vector <Edge> &edges, int src, int dst) {
  9.  
  10.     vector <long long> dist(n + 1, INF);
  11.     dist[src] = 0;
  12.  
  13.     bool ok = 1;
  14.     int step = 0;
  15.  
  16.     while(ok) {
  17.  
  18.         ok = 0;
  19.  
  20.         for(auto &it : edges) {
  21.             if(dist[it.x] + it.cst < dist[it.y]) {
  22.                 dist[it.y] = dist[it.x] + it.cst;
  23.                 ok = 1;
  24.             }
  25.         }
  26.  
  27.         if(step > n) {
  28.             cout << "Ciclu negativ";
  29.             exit(0);
  30.         }
  31.  
  32.         step++;
  33.  
  34.     }
  35.  
  36.     return dist[dst];
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement