Data Structures & AlgorithmsVery hardCodeforces Div 2 C · GATE CS
Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→2 (4), 1→3 (8), 1→4 (4), 2→1 (5), 2→4 (2), 3→1 (6), 3→2 (5), 3→4 (3), 4→1 (8), 4→2 (5), 4→3 (9). 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 1 have finished, what is dist[1][2]?
- A. 3
- B. 5
- C. 4
- D. 6
Correct answer: C. 4
Only intermediate vertices 1 to 1 are allowed in the middle of paths at this point, which gives 4. Stopping one iteration earlier gives 4, and the final distance is 4.
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
- Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→2 (3), 1→4 (9), 2→3 (6), 2→4 (10), 3→1 (12), 3→2 (6), 4→1 (12), 4→2…
- A tree has the vertices F, H, L, N, O, T, U, V, X and the edges L–O, O–F, L–N, F–H, L–V, O–X, H–U, F–T. A centroid is a vertex whose…
- An undirected graph has the vertices E, F, G, I, Z and the edges G–I, G–Z, G–F, E–F, I–Z, I–F, Z–F. A walk may repeat vertices and edges…
- An undirected graph on 5 vertices has the edges D–E, D–J, D–T, E–H, E–J, E–T, H–T, J–T. How many simple paths (no repeated vertex) go from…
- The directed acyclic graph has the vertices D, F, G, I, J, O, Z and the arcs J→D, I→G, G→F, I→Z, I→D, F→D, G→D. How many distinct directed…
- A connected undirected graph has the vertices D, E, O, P, W and weighted edges (weights in brackets): D–P (3), E–P (19), D–E (8), P–O (5)…