Advertisement
Jasir

Bellman ford

May 9th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.18 KB | None | 0 0
  1. vector < vector <pii> > graph(2005);
  2. int vis[2005];
  3.  
  4.  
  5. void bellmanford(int n, int s){
  6.     for(int i=0;i<n;i++) vis[i] = 99999999999999;
  7.     vis[s] = 0;
  8.     for(int i=1;i<n;i++){
  9.         for(int j=0;j<n;j++){
  10.             for(int k=0;k<graph[j].size();k++){
  11.                 vis[graph[j][k].first] = min(vis[graph[j][k].first], vis[j]+graph[j][k].second);
  12.             }
  13.         }
  14.     }
  15.     for(int j=0;j<n;j++){
  16.         for(int k=0;k<graph[j].size();k++){
  17.             if(vis[j]+graph[j][k].second<vis[graph[j][k].first]){
  18.                 cout << "possible" <<endl;
  19.                 return;
  20.             }
  21.         }
  22.     }
  23.     cout << "not possible" <<endl;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement