Data Structures & AlgorithmsModerateGATE CS · Codeforces Div 2 B
A directed graph with no directed cycles has arcs u→v (weight) : U→A (-3), U→V (4), A→Z (3), V→O (2), Z→O (5). Weights may be negative. What is the minimum total weight of a path from U to O that uses at most 2 arcs?
- A. 6
- B. 5
- C. 0
- D. -3
Correct answer: A. 6
Run exactly two Bellman–Ford rounds from a copy of the previous distances: U→V→O = 4 + 2 = 6. The 3-arc path U→A→Z→O = 5 is not allowed, though in-place relaxation can leak it in; 0 stops at Z and −3 is the first arc alone.
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, G, J, O, S, T and the directed edges D→J, G→C, G→O, S→O, C→J, D→O, O→J, D→T, C→O, S→C…
- An undirected graph has the vertices B, C, F, L, O, P, Q, R, V, Z and the edges Q–L, Q–Z, Q–V, B–O, B–L, B–Z, B–R, B–P, O–Z, O–F, O–V…
- A grid has 6 rows and 4 columns (top to bottom; '.' open, '#' blocked): .... #... .... #... ..#. ..#. You may only move right or down, and…
- A bipartite graph has left side P1, P2, P3 and right side Q1, Q2, Q3, Q4, Q5, with edges P1–Q5, P1–Q3, P3–Q4, P1–Q2, P2–Q1, P2–Q4, P3–Q3…
- A directed graph has the vertices E, G, Q, Z and the arcs E→Q, E→Z, G→E, G→Q, G→Z, Q→G, Q→Z, Z→E. Count the directed Hamiltonian paths…
- A grid has 4 rows and 3 columns with these values (top to bottom): 121 725 748 239 A path moves between side-adjacent cells (up, down…