Skip to content

3243. Shortest Distance After Road Addition Queries I #883

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to simulate adding roads between cities and calculate the shortest path from city 0 to city n - 1 after each road addition. Given the constraints and the nature of the problem, we can use Breadth-First Search (BFS) for unweighted graphs.

Approach:

  1. Graph Representation:

    • We can represent the cities and roads using an adjacency list. Initially, each city i will have a road to city i + 1 for all 0 <= i < n - 1.
    • After each query, we add the road from u_i to v_i into the graph.
  2. Shortest Path Calculation (BFS):

    • We can use BFS to calculate the shortest path from city 0 to city n - 1. BFS works well here because all roads have equal weight (each road has a length of 1).
  3. Iterating…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Nov 27, 2024
Collaborator

@mah-shamim
Comment options

mah-shamim Nov 27, 2024
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants