C++ | Dijkstra
Code\ncpp []\nclass Solution {\n    struct comp {\n        bool operator()(auto &A, auto &B) {\n            return A.first > B.first;\n        }\n    };\npublic
# Code\n```cpp []\nclass Solution {\n    struct comp {\n        bool operator()(auto &A, auto &B) {\n            return A.first > B.first;\n        }\n    };\npublic:\n    int minTimeToReach(vector<vector<int>>& moveTime) {\n        priority_queue<pair<int, vector<int>>, vector<pair<int, vector<int>>>, comp> PQ;\n        PQ.push({0, {0, 0, 0}});\n        int n = moveTime.size(), m = moveTime[0].size();\n        vector<vector<int>> dist(n, vector<int>(m, INT_MAX));\n        pair<int, int> dirs[4] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};\n        while(!PQ.empty()) {\n            auto curr = PQ.top();\n            PQ.pop();\n            int currX = curr.second[0], currY = curr.second[1];\n            if(currX == n - 1 && currY == m - 1) return curr.first;\n            for(auto dir: dirs) {\n                int nextX = currX + dir.first, nextY = currY + dir.second;\n                if(nextX < 0 || nextY < 0 || nextX >= n || nextY >= m) continue;\n                int cost = max(curr.first, moveTime[nextX][nextY]) + 1 + curr.second[2]; \n                if(cost < dist[nextX][nextY]) {\n                    dist[nextX][nextY] = cost;\n                    PQ.push({cost, {nextX, nextY, (curr.second[2] + 1) % 2}});\n                }\n            }\n        }\n        return dist[n - 1][m - 1];\n    }\n};\n```
['Graph', 'Heap (Priority Queue)', 'Matrix', 'Shortest Path', 'C++']