Graph Theory37 sections · 1633 units
Open in Course

CSES 1672 Shortest Routes II - Problem Statement

(CSES 1672 walkthrough intro)

The problem asks: given a weighted graph with nn cities, mm roads, and qq queries, answer each query: what is the shortest distance from city aa to city bb?

This is exactly the all-pairs shortest path problem. Run Floyd-Warshall once, then answer each query in O(1)O(1). If no path exists, output 1-1. The constraints allow n500n \leq 500, which makes O(n3)O(n^3) feasible. This problem tests whether you recognize the all-pairs pattern.

Space complexity is O(n2)O(n^2) for the data structures used.