Python3 solution | Dijkstra
IntuitionUsing dijkstra, it's almost identical to3342. Find Minimum Time to Reach Last Room IIand pretty similar to778. Swim in Rising Water.ApproachUse dijkstr
# Intuition
Using dijkstra, it's almost identical to $$ $$***3342. Find Minimum Time to Reach Last Room II***$$ $$ and pretty similar to $$ $$***778. Swim in Rising Water***$$ $$.

# Approach
Use dijkstra to maintain the level of time to reach each room. The time to the next adjacent cell will be when parity is 0:
$$distance[(adj_r, adj_c)] = max(current_d + 1, moveTime[adj_row][adj_col] + 1)$$
and when parity is 1:
$$distance[(adj_r, adj_c)] = max(current_d + 2, moveTime[adj_row][adj_col] + 2)$$
We will maintain parity by starting from 0 and switching between 0 and 1 at each cell.

# Complexity
- Time complexity:
$$O((V+E)logV)$$

- Space complexity:
$$O(V+E)$$

# Code
```python3 []
from queue import PriorityQueue
class Solution:
    def minTimeToReach(self, moveTime: List[List[int]]) -> int:
        distance = defaultdict(lambda: math.inf)
        distance[(0, 0)] = 0
        queue = PriorityQueue()
        queue.put((distance[(0, 0)], 0, 0, 0))
        visited = set()
        directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        n, m = len(moveTime), len(moveTime[0])

        while not queue.empty():
            # distance, row, column
            d, r, c, p = queue.get()

            if r == n - 1 and c == m - 1:
                return distance[(r, c)]

            if (r, c) not in visited:
                visited.add((r, c))

                for dir in directions:
                    next_r, next_c = r + dir[0], c + dir[1]
                    if 0 <= next_r < n and 0 <= next_c < m:
                        if p == 0:
                            if distance[(next_r, next_c)] > max(d, moveTime[next_r][next_c] + 1):
                                distance[(next_r, next_c)] = max(d + 1, moveTime[next_r][next_c] + 1)
                        else:
                            if distance[(next_r, next_c)] > max(d + 2, moveTime[next_r][next_c] + 2):
                                distance[(next_r, next_c)] = max(d + 2, moveTime[next_r][next_c] + 2)

                        queue.put((distance[(next_r, next_c)], next_r, next_c, (p + 1) % 2))
        return distance[(n-1, m-1)]
```
['Python3']