CPP||ModifiedDijikastra
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
#include <bits/stdc++.h>
using namespace std;

int x[] = {1, 0, -1, 0};
int y[] = {0, 1, 0, -1};

struct compare {
    bool operator()(pair<int, pair<long long, pair<int, int>>> a, 
                    pair<int, pair<long long, pair<int, int>>> b) {
        return a.second.first > b.second.first; // Min-heap based on distance
    }
};

class Solution {
public:
    int minTimeToReach(vector<vector<int>>& moveTime) {
        int n = moveTime.size();
        int m = moveTime[0].size();

        vector<vector<bool>> visited(n, vector<bool>(m, false));
        vector<vector<long long>> d(n, vector<long long>(m, LLONG_MAX));

        priority_queue<pair<int, pair<long long, pair<int, int>>>, 
                       vector<pair<int, pair<long long, pair<int, int>>>>, 
                       compare> q;

        d[0][0] = 0;
        q.push({0, {0, {0, 0}}});

        while (!q.empty()) {
            int parity = q.top().first;
            long long dis = q.top().second.first;
            int r = q.top().second.second.first;
            int c = q.top().second.second.second;
            q.pop();

            if (r == n - 1 && c == m - 1) {
                return dis;
            }

            // if (visited[r][c]) continue;
            // visited[r][c] = true;

            for (int i = 0; i < 4; i++) {
                int new_row = r + x[i];
                int new_col = c + y[i];

                if (new_row >= 0 && new_row < n && new_col >= 0 && new_col < m && !visited[new_row][new_col]) {
                    long long min_time = moveTime[new_row][new_col];
                    long long addition = (parity % 2 == 0) ? 1 : 2;

                    // Ensure we reach at least moveTime[new_row][new_col] and align correctly
                    int newd = max(dis,(long long)1* moveTime[new_row][new_col]) + addition;

                    if (newd < d[new_row][new_col]) {
                        d[new_row][new_col] = newd;
                        q.push(make_pair(parity + 1, make_pair(newd, make_pair(new_row, new_col))));
                    }
                }
            }
        }
        return -1; // If no path is found
    }
};

```
['C++']