C++ | Dijkstra on Grid | Easy to understand
Code\ncpp []\nclass Solution {\npublic:\n    int minTimeToReach(vector<vector<int>>& moveTime) {\n        int n=moveTime.size();\n        int m=moveTime[0].size
# Code\n```cpp []\nclass Solution {\npublic:\n    int minTimeToReach(vector<vector<int>>& moveTime) {\n        int n=moveTime.size();\n        int m=moveTime[0].size();\n        vector<pair<int,int>> dir={{0,1},{1,0},{-1,0},{0,-1}};\n        priority_queue< pair<pair<int,int>, pair<int,int>>, vector<pair<pair<int,int>, pair<int,int>>>, greater<pair<pair<int,int>, pair<int,int>>> > pq;\n        vector<vector<int>> dist(n, vector<int>(m,INT_MAX));\n        dist[0][0]=0;\n        pq.push({{0,1},{0,0}});\n\n        while(!pq.empty()){\n            int time=pq.top().first.first;\n            int addTime=pq.top().first.second;\n            int r=pq.top().second.first;\n            int c=pq.top().second.second;\n            pq.pop();\n\n            for(int i=0;i<4;i++){\n                int nr=r+dir[i].first;\n                int nc=c+dir[i].second;\n                if(nr>=0 && nr<n && nc>=0 &&nc<m){\n                    if(time>=moveTime[nr][nc] && time+addTime<dist[nr][nc]){\n                        dist[nr][nc]=time+addTime;\n                        if(addTime==1) pq.push({{dist[nr][nc],2},{nr,nc}});\n                        else pq.push({{dist[nr][nc],1},{nr,nc}});                        \n                    }else if(time<moveTime[nr][nc] && moveTime[nr][nc]+addTime<dist[nr][nc]){\n                        dist[nr][nc]=moveTime[nr][nc]+addTime;\n                        if(addTime==1) pq.push({{dist[nr][nc],2},{nr,nc}});\n                        else pq.push({{dist[nr][nc],1},{nr,nc}});\n                    }\n                }\n            }\n        }\n\n\n        return dist[n-1][m-1];\n\n    }\n};\n```
['C++']