InferenceAI

Enter at least two characters to search.

Start free

Minimum Network Cost

mediumGraphs25 min25 XP

Return the minimum cost to connect all points, where connecting two points costs their Manhattan distance ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2| (a minimum spanning tree).

Requirements

  • Prim's algorithm on the complete graph: O(n2)O(n^2).

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[[0,0],[2,2],[3,10],[5,2],[7,0]]]
TASKminimum_network_cost()
EXPECTED OUTPUT20

Start with the input values shown in this example.

Example 1 of 3

Input: points = [[0,0],[2,2],[3,10],[5,2],[7,0]]

Output: 20

Explanation: classic

Input: points = [[3,12],[-2,5],[-4,1]]

Output: 18

Explanation: three points

Input: points = [[0,0]]

Output: 0

Explanation: single point

Hint 1

Keep the cheapest known connection cost for each point not yet in the tree and add the cheapest one each step.

Topics

DSAGraphsPython