[C++] Dijkstra's Algorithm
Code\ncpp []\nclass Solution {\npublic:\n    int dx[4] = {-1, 1, 0, 0};\n    int dy[4] = {0, 0, -1, 1};\n    const long long INF = 1e15;\n    int minTimeToReach
# Code\n```cpp []\nclass Solution {\npublic:\n    int dx[4] = {-1, 1, 0, 0};\n    int dy[4] = {0, 0, -1, 1};\n    const long long INF = 1e15;\n    int minTimeToReach(vector<vector<int>>& arr) {\n        int n = arr.size();\n        int m = arr[0].size();\n        priority_queue<pair<int, pair<pair<int, int>, int>>, vector<pair<int, pair<pair<int, int>, int>>>, greater<pair<int, pair<pair<int, int>, int>>>> pq;\n        vector<vector<long long>> distance(n, vector<long long>(m, INF));\n        pq.push({0, {{0, 0}, 1}});\n        distance[0][0] = 0;\n        while(!pq.empty()) {\n            auto z = pq.top();\n            int x = z.second.first.first;\n            int y = z.second.first.second;\n            int cost = z.first;\n            int turn = z.second.second;\n            if(x == n - 1 && y == m - 1) {\n                return cost;\n            }\n            pq.pop();\n            for(int j = 0; j < 4; j++) {\n                int newX = x + dx[j];\n                int newY = y + dy[j];\n                if(newX >= 0 && newY >= 0 && newX < n && newY < m) {\n                    \n                    int newDistance = cost + turn;\n                    if(arr[newX][newY] >= cost) {\n                        newDistance = cost + arr[newX][newY] - cost + turn;\n                    }\n                    \n                    if(distance[newX][newY] > newDistance) {\n                        distance[newX][newY] = newDistance;\n                        int newTurn = turn == 1 ? 2 : 1;\n                        pq.push({newDistance, {{newX, newY}, newTurn}});\n                    }\n                }\n            } \n        }\n        // Will not reach here\n        return -1;\n    }\n};\n```
['C++']