[C++] Dijkstra.
\ncpp []\nclass Solution {\npublic:\n    int minTimeToReach(vector<vector<int>>& moveTime) {\n        int n = moveTime.size();\n        int m = moveTime[0].size
\n```cpp []\nclass Solution {\npublic:\n    int minTimeToReach(vector<vector<int>>& moveTime) {\n        int n = moveTime.size();\n        int m = moveTime[0].size();\n\n        priority_queue<array<int, 4>, vector<array<int, 4>>, greater<>> pq;\n        pq.push({0, 0, 0, 1});\n        vector<vector<int>> arriveAt(n, vector<int>(m, INT_MAX));\n        arriveAt[0][0] = 0;\n\n        int dirs[] = {0, 1, 0, -1, 0};\n        while (!pq.empty()) {\n            auto [curTime, y, x, take] = pq.top();\n            pq.pop();\n\n            if (y == n - 1 && x == m - 1) {\n                return curTime;\n            }\n\n            for (int k = 0; k < 4; ++k) {\n                int dy = y + dirs[k];\n                int dx = x + dirs[k + 1];\n\n                if (dy < 0 || dy == n || dx < 0 || dx == m) {\n                    continue;\n                }\n\n                int newTime = max(moveTime[dy][dx], curTime) + take;\n                if (newTime < arriveAt[dy][dx]) {\n                    arriveAt[dy][dx] = newTime;\n                    pq.push({newTime, dy, dx, take == 1 ? 2 : 1});\n                }\n            }\n        }\n\n        return -1;\n    }\n};\n```
['C++']