Advertisement
informaticage

Lazy Dijkstra

Dec 28th, 2019
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <limits>
  5.  
  6. using std::cout;
  7. using std::cin;
  8. using std::vector;
  9. using std::pair;
  10. using std::endl;
  11. using std::greater;
  12. using std::numeric_limits;
  13. using std::priority_queue;
  14.  
  15. long long dijkstra ( vector<vector<pair<int, long long>>> graph, int source, int destination );
  16. void addEdge ( vector<vector<pair<int, long long>>> &graph, int u, int v, int weight );
  17. bool sortByWeight ( const pair<int,int> &p1, const pair<int,int> &p2 );
  18.  
  19. int main ( void )
  20. {
  21.     // freopen("input.txt", "r", stdin);
  22.     // freopen("output.txt", "w", stdout);
  23.     /// Edges nodi, Vertices nodi
  24.     int vertices, edges;
  25.     cin >> vertices >> edges;
  26.  
  27.     int source, destination;
  28.     cin >> source >> destination;
  29.  
  30.     vector<vector<pair<int, long long>>> graph ( vertices );
  31.  
  32.     for ( int i = 0; i < edges; i++ ) {
  33.         int u, v, weight;
  34.         cin >> u >> v >> weight;
  35.         addEdge( graph, u - 1, v - 1, weight );
  36.     }
  37.  
  38.     cout << dijkstra ( graph, source - 1, destination - 1 ) << endl;
  39.     return 0;
  40. }
  41.  
  42. long long dijkstra ( vector<vector<pair<int, long long>>> graph, int source, int destination ) {
  43.     vector <bool> visited ( graph.size(), false );
  44.     vector <long long> distances ( graph.size(), numeric_limits<long long>::max() );
  45.     priority_queue< pair<int, long long>, vector <pair<int, long long>>, greater<pair<int, long long>> > pq;
  46.  
  47.     /// source -> source : 0
  48.     distances [ source ] = 0;
  49.     pq.push( pair<int, long long> ( source, distances [ source ] ) );
  50.  
  51.     while ( ! pq.empty() ) {
  52.         int index = pq.top().first;
  53.         int weight = pq.top().second;
  54.         pq.pop();
  55.  
  56.         visited [ index ] = true;
  57.         for ( pair<int, long long> &edge : graph [ index ] ) {
  58.             if ( ! visited [ edge.first ] ) {
  59.                 long long distance = distances [ index ] + edge.second;
  60.  
  61.                 if ( distance < distances [ edge.first ] ) {
  62.                     distances [ edge.first ] = distance;
  63.                     pq.push( pair<int, long long> ( edge.first, distances [ edge.first ] ) );
  64.                 }
  65.             }
  66.         }
  67.     }
  68.  
  69.     return distances [ destination ];
  70. }
  71.  
  72. void addEdge ( vector<vector<pair<int, long long>>> &graph, int u, int v, int weight ) {
  73.     if ( u >= 0 && u < graph.size() ) {
  74.         if ( v >= 0 && v < graph.size() ) {
  75.             graph [ u ].push_back ( pair <int, int> ( v, weight ) );
  76.             /// IF Undirected
  77.             /// graph [ v ].push_back ( pair <int, int> ( u, weight ) );
  78.             return;
  79.         }
  80.     }
  81. }
  82.  
  83. bool sortByWeight ( const pair<int,int> &p1, const pair<int,int> &p2 ) {
  84.     return ( p1.second < p2.second );
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement