Graph Theory37 sections · 1633 units
Open in Course

CSES 1672 Shortest Routes II - Walkthrough

(Step-by-step trace)

Sample input: n=4n=4, m=3m=3, q=5q=5. Edges: (1,2,5)(1, 2, 5), (1,3,9)(1, 3, 9), (2,3,3)(2, 3, 3). Initialize distdist: dist[1][2] = dist[2][1] = 5, dist[1][3] = dist[3][1] = 9, dist[2][3] = dist[3][2] = 3, rest infinity. After Floyd-Warshall: dist[1][3] = 8 (route 1231 \to 2 \to 3).

Query (1,3)(1, 3) returns 88. This shows how Floyd-Warshall discovers indirect paths that are shorter than direct edges. The algorithm found a better path by routing through vertex 22.