<p>You are given an integer <code>n</code> and a 2D integer array <code>queries</code>.</p>

<p>There are <code>n</code> cities numbered from <code>0</code> to <code>n - 1</code>. Initially, there is a <strong>unidirectional</strong> road from city <code>i</code> to city <code>i + 1</code> for all <code>0 &lt;= i &lt; n - 1</code>.</p>

<p><code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents the addition of a new <strong>unidirectional</strong> road from city <code>u<sub>i</sub></code> to city <code>v<sub>i</sub></code>. After each query, you need to find the <strong>length</strong> of the <strong>shortest path</strong> from city <code>0</code> to city <code>n - 1</code>.</p>

<p>Return an array <code>answer</code> where for each <code>i</code> in the range <code>[0, queries.length - 1]</code>, <code>answer[i]</code> is the <em>length of the shortest path</em> from city <code>0</code> to city <code>n - 1</code> after processing the <strong>first </strong><code>i + 1</code> queries.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5, queries = [[2,4],[0,2],[0,4]]</span></p>

<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>

<p><strong>Explanation: </strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image8.jpg" style="width: 350px; height: 60px;" /></p>

<p>After the addition of the road from 2 to 4, the length of the shortest path from 0 to 4 is 3.</p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image9.jpg" style="width: 350px; height: 60px;" /></p>

<p>After the addition of the road from 0 to 2, the length of the shortest path from 0 to 4 is 2.</p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image10.jpg" style="width: 350px; height: 96px;" /></p>

<p>After the addition of the road from 0 to 4, the length of the shortest path from 0 to 4 is 1.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 4, queries = [[0,3],[0,2]]</span></p>

<p><strong>Output:</strong> <span class="example-io">[1,1]</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image11.jpg" style="width: 300px; height: 70px;" /></p>

<p>After the addition of the road from 0 to 3, the length of the shortest path from 0 to 3 is 1.</p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/28/image12.jpg" style="width: 300px; height: 70px;" /></p>

<p>After the addition of the road from 0 to 2, the length of the shortest path remains 1.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= n &lt;= 500</code></li>
	<li><code>1 &lt;= queries.length &lt;= 500</code></li>
	<li><code>queries[i].length == 2</code></li>
	<li><code>0 &lt;= queries[i][0] &lt; queries[i][1] &lt; n</code></li>
	<li><code>1 &lt; queries[i][1] - queries[i][0]</code></li>
	<li>There are no repeated roads among the queries.</li>
</ul>

class Solution:
    def shortestDistanceAfterQueries(self, n: int, queries: List[List[int]]) -> List[int]:
        # graph = [[0 for column in range(n)] for row in range(n)]
        # dist = [sys.maxsize] * n
        # for i in range(n-1):
        #     graph[i][i+1] = 1
        #     dist[i] = i
        
        # # Initialize shortest distances to infinity
        
        
        
        # def printSolution(dist):
        #     print("Vertex \t Distance from Source")
        #     for node in range(n):
        #         print(node, "\t", dist[node])

        # def minDistance(sptSet):
        #     min = sys.maxsize
        #     for v in range(n):
        #         if dist[v] < min and sptSet[v] == False:
        #             min = dist[v]
        #             min_index = v
        #     return min_index
        
        # def dijkstra(src, end):
        #     sptSet = [False] * n  #Shortest Path Tree Set
        #     for _ in range(end - src + 1):
        #         u = minDistance(sptSet)
        #         sptSet[u] = True
        #         for v in range(n):
        #             if (graph[u][v] > 0 and sptSet[v] == False and dist[v] > dist[u] + graph[u][v]):
        #                 dist[v] = dist[u] + graph[u][v]

        #     # If there's no path to the last node, return a large integer value
        #     if dist[n - 1] == sys.maxsize:
        #         return sys.maxsize
        #     else:
        #         return dist[n - 1]

        # ret = []
        # for st,en in queries:
        #     graph[st][en] = 1
        #     dijkstra(0, en)
        #     ret.append(dist[n-1])
        # return ret
        graph = {i: {i+1: 1} for i in range(n-1)}
        graph[n-1] = {}

        # Process the queries in reverse order
        ret = []
        for u, v in (queries):
            # Add the new road to the graph
            if u in graph:
                graph[u][v] = 1
            else:
                graph[u] = {v: 1}

            # Apply Dijkstra's algorithm
            dist = {i: float('inf') for i in range(n)}
            dist[0] = 0
            heap = [(0, 0)]

            while heap:
                d, node = heapq.heappop(heap)
                if d != dist[node]:
                    continue
                for nei, d2 in graph[node].items():
                    if d + d2 < dist[nei]:
                        dist[nei] = d + d2
                        heapq.heappush(heap, (dist[nei], nei))
            ret.append(dist[n-1])

        return ret
                
2557