Advertisement
sleepy_coder

Bellman_Ford SSSP

Nov 5th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1.  
  2. //note that bellman ford still works for undirected graph bwt if the graph has a -ve edge than means it is a cycle
  3. #include <bits/stdc++.h>
  4. #include <ext/pb_ds/tree_policy.hpp>
  5. #include <ext/pb_ds/assoc_container.hpp>
  6. #include <ext/pb_ds/detail/standard_policies.hpp>
  7. using namespace   std;
  8. using namespace __gnu_cxx;
  9. using namespace __gnu_pbds;
  10.  
  11. typedef long long LL;
  12. typedef vector<LL> VL;
  13. typedef vector<int> VI;
  14. typedef pair< LL, LL >PLL;
  15. typedef pair< int, int > PII;
  16.  
  17. #define     sz                  size()
  18. #define     pb                  push_back
  19. #define     INF                 (1<<30)
  20. #define     MOD                 (1000000007)
  21. #define     PI                  (acos(-1.0));
  22. #define     REP(i,n)            for(__typeof(n) i=0; i<(n); i++)
  23. #define     FOR(i,a,b)          for(__typeof(b) i=(a); i<=(b); i++)
  24. #define     ROF(i,a,b)          for(__typeof(b) i=(b); i>=(a); i--)
  25. #define     FOREACH(it, l)      for(__typeof((l).begin()) it = (l).begin();  it != (l).end(); ++it)
  26.  
  27. //find_by_order(k) -> returns pointer to the k th order element, order_of_key(x) -> returns position of key x
  28. typedef tree< int, null_type, less< int >, rb_tree_tag, tree_order_statistics_node_update > ordered_set;
  29. template<typename T> ostream& operator<<(ostream &os, const vector<T> &t) { LL n = t.size(); REP(i, n) os<<t[i]<<" "; return os; }
  30. template<typename T,typename TT> ostream& operator<<(ostream &os, const pair<T,TT> &t) { return os<<"("<<t.first<<","<<t.second<<")"; }
  31. /**__________________________________________________________________________________________________________________________________*/
  32.  
  33. #define M 10000
  34. VI adj[M], cost[M];
  35. int dis[M], pre[M];
  36. int nodes, edges;
  37.  
  38.  
  39. int bellmanFord(int source, int destination){
  40.     FOR(i, 1, nodes)dis[i] = INF, pre[i] -1;
  41.     dis[source] = 0;
  42.     int cycle = -33;
  43.     FOR(t, 1, nodes-1){
  44.         bool update = false;
  45.         FOR(u, 1, nodes){
  46.             REP(i, adj[u].sz){
  47.                 int v = adj[u][i];
  48.                 if(dis[v] > dis[u] + cost[u][i]){
  49.                     dis[v] = dis[u] + cost[u][i];
  50.                     pre[v] = u;
  51.                     update = true;
  52.                 }
  53.             }
  54.         }
  55.         if(!update)break;
  56.     }
  57.  
  58.     FOR(u, 1, nodes)REP(i, adj[u].sz){
  59.         int v = adj[u][i];
  60.         if(dis[v] > dis[u] + cost[u][i])return cycle;
  61.     }
  62.  
  63.     return dis[destination];
  64. }
  65.  
  66.  
  67. int main(){
  68. #ifdef DEBUG
  69.     freopen("input.txt", "r", stdin);
  70.     freopen("output.txt", "w", stdout);
  71. #endif
  72.     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  73.     //writes your codes here.........................
  74.  
  75.  
  76.     cin>>nodes>>edges;
  77.     FOR(i ,1, edges){
  78.         int u, v, w;
  79.         cin>>u>>v>>w;
  80.         adj[u].pb(v), cost[u].pb(w);
  81.         adj[v].pb(u), cost[v].pb(w);
  82.     }
  83.  
  84.     int s, d;
  85.     cin>>s>>d;
  86.  
  87.     int ans = bellmanFord(s, d);
  88.     if(ans == -33){
  89.         cout<<"Cycle Detected"<<endl;
  90.         assert(ans != -33);
  91.     }
  92.     cout<<ans<<endl;
  93.  
  94.     FOR(i, 1, nodes)cout<<i<<"->"<<dis[i]<<endl;
  95.  
  96.  
  97.  
  98.    
  99.     return 0;
  100.    
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement