Solution using Dijkstra Algorithm
\n# Code\npython3 []\nclass Solution:\n    # solved using dijkstra\'s algorithm, Time complexity: O(Elog(V))\n    def minTimeToReach(self, moveTime: List[List[i
\n# Code\n```python3 []\nclass Solution:\n    # solved using dijkstra\'s algorithm, Time complexity: O(Elog(V))\n    def minTimeToReach(self, moveTime: List[List[int]]) -> int:\n        ROW,COL = len(moveTime), len(moveTime[0])\n        directions = [\n            (-1,0),\n            (1,0),\n            (0,-1),\n            (0,1)\n        ]\n        opp = lambda x: 1 if x == 0 else 0\n        # cost,row,col,add (to handle "takes one second for one move and two seconds for the next, alternating between the two" part)\n        queue = []\n        heapq.heappush(queue,(0,0,0,0))\n        visited = set()\n\n        while len(queue) != 0:\n            cost,row,col,add = heapq.heappop(queue)\n            if row == ROW-1 and col == COL-1:\n                return cost\n            if (row,col) in visited:\n                continue\n\n            visited.add((row,col))\n            for dx,dy in directions:\n                new_row = row + dx\n                new_col = col + dy\n                if (0<=new_row<ROW) and (0<=new_col<COL):\n                    heapq.heappush(\n                        queue,\n                        (\n                            max(cost,moveTime[new_row][new_col]) + 1 + add,\n                            new_row,\n                            new_col,\n                            opp(add)\n                        )\n                    )\n        return -1\n```
['Python3']