mdgaziur001

Dijkstra

Sep 6th, 2026
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. #include <queue>
  5.  
  6. #define INF 1e9
  7.  
  8. using namespace std;
  9.  
  10. pair<vector<int>, vector<int>> dijkstra(vector<vector<pair<int, int>>> &graph, int source) {
  11.     // Conains pairs of distance from source and the node itself
  12.     // <type of data, container of data (we use vector of pairs), comparison operator(greater turns the priority queue into a min priority queue)>
  13.     priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> queue;
  14.     queue.push({ 0, source }); // distance from source to source is zero
  15.  
  16.     // stores distance from source
  17.     vector<int> distances(graph.size(), INF);
  18.     distances[source] = 0; // distance from source to source is zero
  19.    
  20.     // Initialize with -1, indicating that we haven't found the optimal
  21.     // parent node for every node
  22.     vector<int> parents(graph.size(), -1);
  23.  
  24.     while (!queue.empty()) {
  25.         // Pop the closest undiscovered node
  26.         pair<int, int> current_node = queue.top();
  27.         queue.pop();
  28.  
  29.         int dist = current_node.first;
  30.         int node = current_node.second;
  31.    
  32.         // We do this to avoid processing stale distances.
  33.         // Suppose, we push { 10, B } into the queue.
  34.         // Before we reach this path, suppose we find a better path { 5, B } and push
  35.         // it. As it is a min priority queue, { 5, B } will now be popped before { 10, B }.
  36.         // Now, we can clearly see that there's a stale path { 10, B } we do not need to process. { 5, B } will set B's source distance to 5... which is less than 10. So, this check will skip the useless iteration over B's neighbors through the stale path.
  37.         if (dist > distances[node]) continue;
  38.  
  39.         // Iterate over every neighbor of the node
  40.         for (auto &edge: graph[node]) {
  41.             int neighbor = edge.first;
  42.             int distance = edge.second;
  43.  
  44.             // Check if distance from current node to this neighbor is smaller than what we recorded before... if smaller, we store the new path.
  45.             int new_distance = dist + distance;
  46.             if (new_distance < distances[neighbor]) {
  47.                 distances[neighbor] = new_distance;
  48.                 queue.push({ distances[neighbor], neighbor });
  49.  
  50.                 // the current node to neighbor distance is the smallest, so we reach this neighbor by current node.
  51.                 parents[neighbor] = node;
  52.             }
  53.         }
  54.     }
  55.  
  56.     return { distances, parents };
  57. }
  58.  
  59. vector<int> construct_path(vector<int> &parents, int target) {
  60.     vector<int> path;
  61.  
  62.     // We keep following the parent of target node and push
  63.     // until we reach the source node(it's parent is -1)
  64.     while (target != -1) {
  65.         path.push_back(target);
  66.         target = parents[target];
  67.     }
  68.  
  69.     // The path is reversed as we started from the target node,
  70.     // so we reverse the node.
  71.     reverse(path.begin(), path.end());
  72.  
  73.     return path;
  74. }
  75.  
  76. int main() {
  77.     int nodes, edges, source;
  78.     cin >> nodes >> edges >> source;
  79.  
  80.     vector<vector<pair<int, int>>> graph(nodes + 1);
  81.  
  82.     for (int i = 1; i <= edges; i++) {
  83.         int from, to, dis;
  84.         cin >> from >> to >> dis;
  85.  
  86.         graph[from].push_back({ to, dis });
  87.     }
  88.  
  89.     // C++ syntax to destructure a pair, nothing too fancy
  90.     auto [ distances, parents ] = dijkstra(graph, source);
  91.    
  92.     for (int i = 1; i <= nodes; i++) {
  93.         if (parents[i] == -1 && i != source) {
  94.             cout << i << "(INF)" << endl;
  95.             continue;
  96.         }
  97.         vector<int> path = construct_path(parents, i);
  98.         for (int j = 0; j < path.size() - 1; j++) {
  99.             cout << path[j] << " -> ";
  100.         }
  101.         cout << i << "(" << distances[i] << ")" << endl;
  102.     }
  103.  
  104.     return 0;
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment