Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional
Directed weighted graph on vertices D, C, E, F, B (source D, target B) with edges: D→C (6); D→E (7); E→C (-4); C→B (4); E→B (5); D→B (11); D→F (5). Run Dijkstra from D with a min-heap: a vertex is finalised the first time it is popped, its outgoing edges are relaxed then, and a finalised vertex's distance is never changed afterwards (edges into finalised vertices are ignored). What distance to B does it report?
- A. 11
- B. 7
- C. 12
- D. 10
Correct answer: D. 10
Dijkstra finalises C at distance 6 before the negative edge from E is seen, so it reports 10. The true shortest distance is 7, which goes through the negative edge; clamping negatives to 0 or using their absolute values are other wrong models.
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
- Undirected simple graph on vertices A, B, C, D, E, F with edges A–B (1), A–C (1), A–D (1), A–F (1), B–C (1), B–D (1), B–E (1), B–F (1)…
- A chain of 5 matrices M1 to M5 has dimensions given by the sequence p = [8, 7, 16, 17, 9, 9], where Mi has size p[i−1] × p[i] (1-indexed)…
- Directed acyclic graph on vertices A, B, C, D, E, F, G (every edge goes from a smaller to a larger letter) with weighted edges A→E (2)…
- Directed acyclic graph on vertices A, B, C, D, E, F, G (every edge goes from a smaller to a larger letter) with weighted edges A→G (4)…
- Directed weighted graph on vertices A, B, C, D with edges C→B (-4); C→A (-1); A→C (1); D→A (-4); A→D (-2); C→D (0). Source A. Run…
- Connected undirected graph on vertices A, B, C, D, E, F, G, H with edges A–E (1), B–D (1), B–E (1), C–H (1), D–G (1), D–H (1), E–F (1)…