C++ | Code Explained With Proper Comment | Dijkstra Algorithm
\n// 1. DijkstraAlgorithm => TC O(nlogn) | SC O(n)\nclass Solution {\npublic:\n    int networkDelayTime(vector<vector<int>>& times, int n, int k) {\n        pri
```\n// 1. DijkstraAlgorithm => TC O(nlogn) | SC O(n)\nclass Solution {\npublic:\n    int networkDelayTime(vector<vector<int>>& times, int n, int k) {\n        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;  // min heap\n        pq.push({0, k});\n        vector<vector<pair<int, int>>> adj(n + 1);  // declare adjacency matrix for easy node access\n        for(auto it : times){  // initialize adjacency matrix\n            adj[it[0]].push_back({it[1], it[2]});\n        }\n        vector<int> wt(n + 1, INT_MAX);  // saving weight of each node to infinity\n        wt[k] = 0;   // weight of source is zero\n        while(!pq.empty()){  // dijkstra algo to find min distance of each node from source\n            int prevDis = pq.top().first;\n            int node = pq.top().second;\n            pq.pop();\n            for(auto it: adj[node]){\n                int adjNode = it.first;\n                int adjDis = it.second;\n                if(wt[adjNode] > prevDis + adjDis){\n                    wt[adjNode] = prevDis + adjDis;\n                    pq.push({prevDis + adjDis, adjNode});\n                }\n            }\n        }\n        int maxi = -1;\n        for(int i = 1; i <= n; i++){\n            if(wt[i] == INT_MAX) return -1; // if node consist INT_MAX, means all nodes not visited\n            maxi = max(wt[i], maxi);\n        }\n        return maxi;\n    }\n};\n\n// please upvote\n// if you find this helpful \n```
['C++']