BotByte

Bellmon Ford.cpp

Feb 22nd, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. /***Bellmon Ford***/
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6. typedef pair<int, pair<int, int> > PII;
  7. #define MAX 1000009
  8.  
  9. vector<PII> adj[MAX];
  10. int dist[MAX];
  11. int N, E;
  12.  
  13. void bellmonFord()
  14. {
  15.     for(int i=1; i<=N-1; i++){
  16.         for(int j=0; j<E; j++){
  17.             int u = adj[j][0].second.first;
  18.             int v = adj[j][0].second.second;
  19.             int cost = adj[j][0].first;
  20.             if(dist[u] + cost < dist[v]) dist[v] = dist[u] + cost;
  21.         }
  22.     }
  23.     for(int i=2; i<=N; i++) printf("%d ", dist[i]);
  24. }
  25.  
  26. int main()
  27. {
  28.     scanf("%d %d", &N, &E);
  29.     dist[1] = 0;
  30.     for(int i=2; i<=N; i++) dist[i] = 1e9;
  31.     for(int i=0; i<E; i++){
  32.         int u, v, w;
  33.         scanf("%d %d %d", &u, &v, &w);
  34.         adj[i].push_back(make_pair(w, make_pair(u, v)));
  35.     }
  36.     bellmonFord();
  37. }
Advertisement
Add Comment
Please, Sign In to add comment