Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
Floyd–Warshall runs on vertices 1 to 5 with these arcs u→v (weight): 1→2 (4), 1→3 (2), 2→1 (11), 3→1 (8), 3→2 (4), 3→4 (7), 3→5 (4), 4→1 (7), 4→2 (2), 4→3 (8), 4→5 (1), 5→2 (1), 5→3 (5), 5→4 (4). 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 4 have finished, what is dist[4][2]?
- A. 3
- B. 1
- C. 2
- D. 4
Correct answer: C. 2
Only intermediate vertices 1 to 4 are allowed in the middle of paths at this point, which gives 2. Stopping one iteration earlier gives 2, and the final distance is 2.
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
- Bellman–Ford is run from source U on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: K→U…
- A four-wheel combination lock shows four digits 0–9 on wheels that wrap around (9 goes to 0 and 0 goes to 9). One move turns one wheel by…
- A tree has vertices with weights in brackets: D[15], C[15], R[7], L[9], S[1], Z[3], B[13], H[13], F[6]. The edges are D–C, D–R, R–L, D–S…
- A tree on the vertices C, F, H, L, P, V, W has the arcs V→P, F→P, P→C, F→H, V→L, W→H (each undirected edge is directed as shown). You may…
- Each cell of a grid with 3 rows and 4 columns has an arrow: R = right, L = left, U = up, D = down. RRLL URUU RRUD From a cell you may move…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 18. Two sequences of steps…