Data Structures & AlgorithmsHardCodeforces Div2 D · shortest path
Problem: After running Floyd-Warshall on a directed graph with possibly negative edges, which check correctly detects that vertex i lies on a negative cycle?
- A. dist[i][i] < 0
- B. dist[0][i] < 0
- C. dist[i][i] == i
- D. dist[i][j] < 0 for every j
Correct answer: A. dist[i][i] < 0
If i lies on a negative cycle, the DP can route a path from i back to i with negative total weight, so dist[i][i] becomes negative. A negative distance from vertex 0 alone does not imply a cycle.
More Coding Interview Problems questions
- Problem: A road graph has V = 10^5 vertices and E = 3 10^5 edges, all weights non-negative, and you need shortest paths from one source…
- Problem: For the shortest subarray with sum at least S in an array of positive integers, a two-pointer sweep is used: extend the right…
- Problem: Weighted interval scheduling (each job has a start, finish and profit; choose a set of non-overlapping jobs to maximise profit)…
- Problem: What does the following print? It returns the next greater element of each position in a circular array (search continues from…
- Problem: How many cells of a 3 x 3 grid are at exactly 2 steps (4-directional moves, no obstacles) from the top-left cell? Run the BFS…
- Problem: How many ordered pairs (a, b) with 1 <= a, b <= 6 have gcd(a, b) = 1? python from math import gcd print(sum(1 for a in range(1…