C++ Clean Code|| Dijkstra’s Algorithm || Self Explanatory
Intuition\nJust Simple Dijkstra\u2019s implementation we try to go to all nodes possible from current code and if we can\'t reach there at given time than we as
# Intuition\nJust Simple Dijkstra\u2019s implementation we try to go to all nodes possible from current code and if we can\'t reach there at given time than we assume that travel back and forth and increase time, and if value of new time for that node is less than the current value in resultant vector than update and add that in priority queue.\n\n# Complexity\n- Time complexity:\nO( E log(V) ) { for Dijkstra\u2019s Algorithm }\n\n- Space complexity:\nO( |E| + |V| ) { for priority queue and dist array } + O( |V| ) { for storing the final path }\n\nWhere E = Number of edges and V = Number of Nodes.\n\n# Code\n```\nclass Solution {\npublic:\n    int minimumTime(vector<vector<int>>& grid) \n    {\n        int n = grid.size();\n        int m = grid[0].size();\n        vector<vector<int>> res(n,vector<int>(m,1e9));\n        \n        if(grid[0][1]>grid[0][0]+1 && grid[1][0]>grid[0][0]+1)\n        {\n            return -1;\n        }\n        \n        priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> pq;\n        \n        pq.push({0,{0,0}});\n        \n        int nr[] = {-1,0,1,0};\n        int nc[] = {0,1,0,-1};\n        \n        while(!pq.empty())\n        {\n            auto it = pq.top();\n            pq.pop();\n            int time = it.first;\n            int r = it.second.first;\n            int c = it.second.second;\n            \n            for(int i=0;i<4;i++)\n            {\n                int x = r + nr[i];\n                int y = c + nc[i];\n                \n                if(x>=0 && x<n && y>=0 && y<m)\n                {\n                    int ntime;\n                    \n                    if(time + 1 < grid[x][y])\n                    {\n                        ntime = grid[x][y];\n                        \n                        if(((time+1)+ntime)%2)\n                        {\n                            ntime++;\n                        }\n                    }\n                    \n                    else\n                    {\n                        ntime = time+1;\n                    }\n                    \n                    // We will go back and forth with prev. node till time will be \n                    // greater or equal\n                    if(ntime < res[x][y])\n                    {\n                        res[x][y]=ntime;\n                        pq.push({ntime,{x,y}});\n                    }\n                }\n            }\n        }\n        \n        if(res[n-1][m-1]==INT_MAX)\n        {\n            res[n-1][m-1]=-1;\n        }\n        return res[n-1][m-1];\n    }\n};\n```
['C++']