[C++] Dijkstra only code
{:style="width:440px"}\n\n---\ncpp [easyAF\uD83D\uDE3C.cpp]\nclass Solution {\npublic:\n    int minimumTime(vector<vector<int>>& grid) {\n\n        int m = grid
![upVote.jpeg](https://assets.leetcode.com/users/images/f9dcf763-6097-45fa-8954-be06596e730a_1732881229.0581257.webp){:style="width:440px"}\n\n---\n```cpp [easyAF\uD83D\uDE3C.cpp]\nclass Solution {\npublic:\n    int minimumTime(vector<vector<int>>& grid) {\n\n        int m = grid.size(), n = grid[0].size();\n        if (grid[0][1] > 1 && grid[1][0] > 1)\n            return -1;\n\n        vector<vector<int>> ans(m, vector<int>(n, INT_MAX));\n        vector<pair<int, int>> direc = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};\n\n        priority_queue<pair<int, pair<int, int>>> pq; // max heap in c++\n        // storing time in negative values to make it act like a min heap for\n        // time.\n        pq.push({0, {0, 0}});\n        while (pq.size()) {\n\n            auto it = pq.top();\n            pq.pop();\n\n            for (auto e : direc) {\n\n                int x = it.second.first + e.first,\n                    y = it.second.second + e.second;\n\n                if (x < 0 || y < 0 || x >= m || y >= n)\n                    continue;\n\n                int time = -it.first + 1;\n\n                if (time < grid[x][y])\n                    time = grid[x][y] + ((grid[x][y] - time) & 1); // waiting takes 2 secons (back n forth)\n\n                if (time < ans[x][y]) {\n                    ans[x][y] = time;\n                    pq.push({-time, {x, y}});\n                }\n            }\n        }\n\n        return ans[m - 1][n - 1];\n    }\n};\n```
['Breadth-First Search', 'Heap (Priority Queue)', 'C++']