Graph Theory37 sections · 1633 units
Open in Course

CSES 1672 Shortest Routes II - Lessons

(Precomputation wins)

This problem teaches you that precomputation wins when you have many queries.

Instead of running Dijkstra qq times, run Floyd-Warshall once and answer queries in O(1)O(1). This pattern applies to many problems: if you can afford O(n3)O(n^3) preprocessing, you can answer all-pairs queries immediately. The core insight is recognizing when the query count justifies expensive preprocessing. If q>nq > n, Floyd-Warshall is usually the right choice.