Data Structures & AlgorithmsVery hardCodeforces Div 2 C · GATE CS
Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→3 (1), 1→4 (4), 2→1 (1), 2→3 (3), 3→1 (12), 3→2 (11), 3→4 (3), 4→1 (9), 4→3 (3). dist[i][i] = 0, dist[i][j] is the arc weight, or ∞ when there is no arc. The outer loop runs over k = 1, 2, … in that order. After the iterations for k = 1 through 3 have finished, what is dist[1][2]?
- A. ∞
- B. 11
- C. 12
- D. 18
Correct answer: C. 12
After rounds k = 1..3, only vertices 1 to 3 may sit inside a path. Vertex 1 reaches 2 only through 3: 1→3 (1) + 3→2 (11) = 12, available once k = 3 has run. Stopping after k = 2 leaves ∞, 11 reads just the arc 3→2, and 18 detours through vertex 4, which is not yet allowed and longer anyway.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A directed acyclic graph has the vertices C, D, H, I, U, X and the arcs U→I, U→H, U→C, D→I, D→C, X→I, X→C, H→C. Two players alternately…
- A grid has 6 rows and 8 columns (rows listed top to bottom; '.' is an open cell, '#' a wall): ..#..... .#.....# ...##... ....###. #.###…
- Two unmarked jugs hold 5 litres and 9 litres. Allowed operations: fill a jug completely, empty a jug completely, or pour one jug into the…
- A directed graph with no directed cycles has arcs u→v (weight) : X→E (-1), X→A (-3), X→J (8), E→G (-1), A→G (7), A→J (0), G→J (-3)…
- A directed acyclic graph has the vertices F, N, R, S, T, U, V, X and the directed edges R→V, U→S, S→X, F→X, N→V, R→S, U→F, F→N, R→F, R→T…
- An undirected graph has the vertices B, E, G, I, J, K, O, P and the edges I–J, I–O, J–E, J–G, J–P, E–K, E–B, G–P, G–B, G–O, K–P, K–B. What…