Advertisement
oaktree

dijkstra-adjacency-list.cpp

May 24th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. /*
  2.     DIJKSTRA'S ALGO WITH ADJACENCY LIST
  3. */
  4. #include <vector>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <limits>
  8. #include <utility>
  9.  
  10. /* typedef to make more clear */
  11. typedef std::vector< std::vector< std::pair<int,int> > > adjacency_list;
  12.  
  13. int dijkstra(const adjacency_list& adj_list, const int& start, const int& end, std::vector<int>& distances);
  14.  
  15. int main() {
  16.     int n,m; std::cin >> n >> m;
  17.    
  18.     /*
  19.         adj-list using vector of size n holding a vector for
  20.         each vertex.
  21.     */
  22.     adjacency_list adj_list(n);
  23.    
  24.     int f,s,w; /* first, second, weight */
  25.     for (int i = 0; i < m; i++) {
  26.         std::cin >> f >> s >> w;
  27.  
  28.         /* graph is undirected */
  29.         adj_list[f-1].push_back( std::make_pair(s-1,w) );
  30.         adj_list[s-1].push_back( std::make_pair(f-1,w) );
  31.     }
  32.    
  33.     int start; std::cin >> start;
  34.     start--; /* zero-base the starting point */
  35.  
  36.     std::vector<int> distances(adj_list.size(), std::numeric_limits<int>::max());
  37.     for (int i = 0; i < n; i++) {
  38.         if (i != start)
  39.             std::cout << dijkstra(adj_list, start, i, distances) << " ";
  40.     }
  41.     std::cout << std::endl;
  42.    
  43.     return 0;
  44. }
  45.  
  46. int dijkstra(const adjacency_list& adj_list, const int& start, const int& end, std::vector<int>& distances) {
  47.    
  48.     /* another vec of ints to keep track of traversable nodes*/
  49.     std::vector<int> nodes;
  50.    
  51.     distances[start] = 0;
  52.    
  53.     for (int i = 0, n = adj_list.size(); i < n; i++)
  54.         nodes.push_back(i);
  55.    
  56.     nodes.push_back(start);
  57.    
  58.     auto comp = [&](int l, int r) {return distances[l] > distances[r];};
  59.     std::make_heap(nodes.begin(), nodes.end(), comp);
  60.    
  61.     while(!nodes.empty()) {
  62.         std::pop_heap(nodes.begin(), nodes.end(), comp);
  63.         int smallest = nodes.back();
  64.         nodes.pop_back();
  65.  
  66.         if (smallest == end)
  67.             break; /* returns at bottom anyway */
  68.  
  69.         if (distances[smallest] == std::numeric_limits<int>::max())
  70.             break;
  71.  
  72.         for(int i = 0, n = adj_list[smallest].size(); i < n; i++) {
  73.             int tmp = distances[smallest] + adj_list[smallest][i].second;
  74.            
  75.             if (tmp < distances[adj_list[smallest][i].first] && tmp > 0) {
  76.                 distances[adj_list[smallest][i].first] = tmp;
  77.                 std::make_heap(nodes.begin(), nodes.end(), comp);
  78.             }
  79.         }
  80.     }
  81.     if (distances[end] == std::numeric_limits<int>::max())
  82.         return -1;
  83.     else
  84.         return distances[end];
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement