Strain Calibration Destinations

Minimum cost and lowest-index nearest target per directed graph node

MediumGraphsMulti-Source Shortest PathsDijkstra

A genetics lab stores strains numbered from 0 to n - 1. Some strains are certified calibration references.

Each entry [a, b, cost] in conversions describes a directed procedure that converts strain a into strain b at the given nonnegative cost. Procedures may be chained, and the total cost is the sum of their costs. A procedure cannot be used backward unless a separate reverse procedure is listed.

For each starting strain, find the certified reference it can reach at minimum total cost. If several references have the same minimum cost, choose the reference with the smallest strain number. A strain can remain unchanged at cost 0, so a certified strain can reach itself without using any procedure.

Return a list of n pairs in starting-strain order. Each pair is [minimum_cost, reference_number]. If no certified reference is reachable, use [-1, -1].

Examples

Example 1

Input: n = 5, conversions = [[0,1,2],[1,2,3],[0,3,5],[1,3,4]], references = [3,2]
Output: [[5,2],[3,2],[0,2],[0,3],[-1,-1]]

Strain 0 can reach reference 2 through strain 1 for the same total cost as its direct procedure to reference 3, so it chooses reference 2. Strain 1 also chooses reference 2. Both references can remain unchanged, while strain 4 cannot reach either reference.

Example 2

Input: n = 3, conversions = [[0,1,0],[1,0,0],[2,1,4]], references = [1,0]
Output: [[0,0],[0,0],[4,0]]

The zero-cost procedures between strains 0 and 1 let both reach either certified reference at cost zero. The tie rule makes both choose reference 0. Strain 2 reaches the same chosen reference through strain 1.

Example 3

Input: n = 3, conversions = [[0,1,2],[1,2,1]], references = []
Output: [[-1,-1],[-1,-1],[-1,-1]]

There are no certified references, so no starting strain has a valid calibration destination, even though conversions exist.

Constraints

  • 1 <= n <= 3000
  • 0 <= conversions.length <= 6000
  • Each conversion is [a, b, cost], where 0 <= a, b < n and 0 <= cost <= 1000000.
  • Self-conversions and multiple procedures between the same ordered pair are allowed.
  • 0 <= references.length <= n
  • references contains distinct strain numbers in the range [0, n - 1].

The expected running time is O((n + m) log(n + m)), where m is the number of conversions, with O(n + m) auxiliary space. All costs and answers fit exactly in JavaScript's safe integer range.

Hints

Show hint 1

Reverse every procedure. A search starting at a reference in the reversed graph describes strains that can reach that reference in the original graph.

Show hint 2

Start a single shortest-path search from all references. Compare candidate labels by total cost first and reference number second, including when a procedure has zero cost.

Follow-up questions

What an interviewer might ask once you have a working solution.

  • How would you additionally return the fewest procedures among routes with the selected minimum cost and reference?
  • If every procedure cost were either zero or one, could you avoid a binary heap while preserving the reference-number tie rule?

Practice this with an AI interviewer

Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.

Start this problem