Easy C++ Solution | Dijkstras Algo | Explained
\nclass Solution {\npublic:\n    int networkDelayTime(vector<vector<int>>& times, int n, int k) {\n        vector <vector<pair<int, int>>> neighs(n+1);       //
```\nclass Solution {\npublic:\n    int networkDelayTime(vector<vector<int>>& times, int n, int k) {\n        vector <vector<pair<int, int>>> neighs(n+1);       //to store the neighbors and weights corresponding to each vertices\n        for(auto t: times)                                 //pushing vertex, neighbor, weight in the proper format\n            neighs[t[0]].push_back({t[1], t[2]});\n            \n        vector <int> res(n+1, INT_MAX);                    //to store the time taken by signal to reach each vertices\n        priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;    //min heap\n        pq.push({0, k});                                   //starting with the starting vertex\n        res[k]=0;                                          //since no time is required for starting vertex to reach itself\n        \n        while(!pq.empty()){\n            int vertex=pq.top().second;     \n            pq.pop();\n            \n            for(auto neigh:neighs[vertex]){                //considering each neighbors\n                //if time taken to reach end point(neigh.first) is more than time taken to reach \n                //starting point(i.e vertex) initially+time taken(i.e neigh.second) to reach from\n                //starting to end with another path\n                if(res[vertex]+neigh.second<res[neigh.first]){  \n                    //updating time for shorter path\n                    res[neigh.first]=res[vertex]+neigh.second;\n                    //pushing for the further journey from end point(i.e neigh.first)\n                    pq.push({res[neigh.first], neigh.first});\n                }\n            }\n        }\n        \n        int ans=*max_element(res.begin()+1, res.end());     //to get max time taken\n        //if it is impossible for all nodes to receive signal, i.e we still have INT_MAX in res \n        //array, so return -1. Else return ans\n        return ans==INT_MAX?-1:ans;                         \n    }\n};\n```
['Graph', 'C', 'Heap (Priority Queue)', 'C++']