Data Structures & AlgorithmsVery hardICPC regional · graph round
Problem: A solver runs Dijkstra from A on a directed graph. Which graph makes Dijkstra output a wrong shortest distance?
- A. Edges A to B weight 2, A to C weight 3, C to B weight 1 (all non-negative)
- B. Edges A to B weight 4, B to C weight -1, A to C weight 3
- C. Edges A to B weight 1, A to C weight 5, C to B weight -10
- D. Edges A to B weight -1 and B to A weight 5 (no negative cycle)
Correct answer: C. Edges A to B weight 1, A to C weight 5, C to B weight -10
Dijkstra finalises B with distance 1 before C is relaxed, but the path A to C to B has weight 5 - 10 = -5. Negative edges break the invariant that a finalised vertex is optimal.
More Coding Interview Problems questions
- Problem: What does the following print? python a = list(range(10)) print(a[::-3], a[2:8:4])
- Problem: Count subarrays whose sum is divisible by k=5 for a = [4, 5, 0, -2, -3, 1]. python from collections import Counter def f(a, k): c…
- Problem: Kadane's algorithm is started with best = 0 and a running sum initialised to 0, then applied to an array where every number is…
- Problem: You binary search the answer x for 'the minimum possible largest sum when an array is split into k contiguous parts'. Which…
- Problem: What is the time complexity of this loop in terms of n? python for i in range(1, n + 1): for j in range(i, n + 1, i): work()
- Problem: Count right/down paths from the top-left to the bottom-right of the 3x3 grid below, where '#' is blocked. The code returns the…