Data Structures & AlgorithmsModerateCodeforces Div 2 D · ICPC regional
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 other until the source is empty or the destination is full. Both jugs start empty. What is the minimum number of operations needed so that one of the jugs contains exactly 3 litres?
- A. 10
- B. 8
- C. 4
- D. 2
Correct answer: B. 8
BFS over (first jug, second jug) states gives 8 operations: fill the 9, pour into the 5, empty the 5, pour, fill the 9, pour, empty, pour. Requiring the target in the 5-litre jug takes 10; 4 counts only the pours and 2 only the fills of the optimal route.
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 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…
- An undirected connected graph on the vertices E, I, L, Y, Z has the edges Y–I, Y–Z, E–Y, L–Z, E–Z, E–I. How many spanning trees does it…
- A bipartite graph has left side P1, P2, P3, P4 and right side Q1, Q2, Q3, Q4, with edges P2–Q3, P2–Q1, P2–Q2, P3–Q4, P1–Q3, P3–Q3. What is…
- Bellman–Ford is run from source T on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: P→Y…