Modified Dijkstra Solution || C++
We will be using dijkstra algorithm to find minimum time to reach bottom right from top left.\n\n\n\n\n\n\n\ntypedef pair<int,int> pi;\nclass Solution {\npublic
We will be using dijkstra algorithm to find minimum time to reach bottom right from top left.\n\n![image](https://assets.leetcode.com/users/images/5dffba17-04e2-4c7f-a2d0-3b5beb3ca38f_1677385772.468185.png)\n![image](https://assets.leetcode.com/users/images/d7d2ae94-ca07-46f0-9e4c-733abfc89b5f_1677385887.5832057.png)\n\n\n\n```\ntypedef pair<int,int> pi;\nclass Solution {\npublic:\n    \n    int isSafe(int x, int y, int m, int n){\n        \n\t\t//returns whether a point lies within the grid or not\n        if(x<0 or y<0 or x==m or y==n)\n            return 0;\n        \n        return 1;\n        \n    }\n    int minimumTime(vector<vector<int>>& grid) {\n\t\t\t\n\t\t// If the neighbouring cells of grid[0][0] is not 1, we will be not able to travel forward so just return -1\t\n        if(grid[1][0]>1 and grid[0][1]>1)\n            return -1;\n        \n\t\t// priority queue\n\t\t// 1st value in pair - Time required to reach that point\n\t\t// 2nd pair - Coordinates (x,y)\n\n        priority_queue<pair<int,pi>,vector<pair<int,pi>>,greater<pair<int,pi>>> pq;\n        pq.push({0,{0,0}});\n        int m = grid.size();\n        int n = grid[0].size();\n        \n\t\t// Dist vector of vector to store min time required to reach a point\n        vector<vector<int>> dist(m,vector<int> (n,INT_MAX));\n\t\t\n\t\t// Defining available moves\n        vector<vector<int>> moves = {{1,0},{-1,0},{0,1},{0,-1}};\n        \n        while(!pq.empty()){\n            \n            auto tmp = pq.top();\n            pq.pop();\n            \n            int val = tmp.first;\n            int x = tmp.second.first;\n            int y = tmp.second.second;\n            \n\t\t\t//If we have reached the right bottom return the min time\n            if(x == m-1 and y == n-1)\n                return val;\n            \n            for(int i=0;i<4;i++){\n                \n                int nx = x+moves[i][0];\n                int ny = y+moves[i][1];\n                \n                if(!isSafe(nx,ny,m,n))\n                    continue;\n                \n\t\t\t\t// Finding the difference between time of the cells\n                int currDist = grid[nx][ny] - val;\n                \n                currDist = max(currDist,0);\n                \n\t\t\t\t//If the distance between the times of 2 cell is even then we have to increment required time\n                if(currDist%2 == 0)\n                    currDist++;\n                \n                if(dist[nx][ny]>val+currDist)\n                {\n                    dist[nx][ny] = val+currDist;\n                    pq.push({dist[nx][ny],{nx,ny}});\n                }\n                \n            }\n            \n        }\n        return -1;\n        \n        \n    }\n};\n\n```
['Heap (Priority Queue)']