Python | Dijkstra’s Algorithm
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfrom heapq import heappush,heappop\nclass Solution:\n    def minimumTime(self, grid: List[List[int]]) -> int:\n        R = len(grid)\n        C = len(grid[0])\n        # Check if there is a path exist\n        has_path = False\n        for r,c in ((0,1),(1,0)):\n            if 0<=r<R and 0<=c<C:\n                if grid[r][c] <= 1 :\n                    has_path = True\n        if not has_path : return -1\n        heap = [(0,0,0)]\n        dist = {}\n        dist[(0,0)] = 0\n        visited = set()\n        while heap:\n            _,r,c = heappop(heap)\n            if (r,c) in visited: continue\n            if r == R-1 and c == C-1:\n                break\n            visited.add((r,c))\n            cur_time = dist[(r,c)]\n            for nr,nc in ((r+1,c),(r-1,c),(r,c+1),(r,c-1)):\n                if (nr,nc) in visited : continue\n                if 0<=nr<R and 0<=nc<C:\n                    cost = None\n                    if grid[nr][nc] <= cur_time + 1:\n                        cost = 1\n                    else:\n                        cost = grid[nr][nc] - cur_time\n                        if cost%2 == 0 :\n                            cost += 1\n                    if cost:\n                        if cur_time + cost < dist.get((nr,nc),float(\'inf\')):\n                            dist[(nr,nc)] = cur_time + cost\n                            heappush(heap,(dist[(nr,nc)],nr,nc))\n        return dist[(R-1,C-1)]\n            \n```
['Python3']