Hacker Land
Practice
3.2 (5 votes)
Graphs
Algorithms
Minimum spanning tree
Problem
60% Success 1024 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

There are total \(N\) Hacker-cities in a plane. Each city is located on coordinates \((X[i], Y[i])\) and there can be any number of cities on the same coordinates.

You have to make these cities connected by constructing some roads in such a way that it is possible to travel between every pair of cities by traversing the roads. The cost of constructing one road between any two cities is the minimum of the absolute difference between their \(X\) and \(Y\) coordinates.

As you want to earn more and more, you decided to do this in the most optimal way possible, such that the total cost of constructing these roads is minimal. You have to return the minimum money you need to spend on connecting all the cities.

Input Format:

  • The first line contains an integer \(T\), denoting the number of test cases.
  • The first line of each test case contains a single integer \(N\) denoting the number of cities.
  • The second line of each test case contains \(N\) space-separated integers denoting the \(X\) coordinates of the \(N\) cities.
  • The third line of each test case contains \(N\) space-separated integers denoting the \(Y\) coordinates of the \(N\) cities

Output Format:

For each test case, print the minimum money needed to spend in connecting all the cities.

Constraints:

\(1 <= T <= 10\)

\(2 <= N <= 10^5\)

\(0 <= X[i],Y[i] <= 10^9\)

The sum of \(N\) over all test cases does not exceed \(10^5\).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
15 votes
Tags:
AlgorithmsGraphsMediumMinimum spanning tree
Points:30
32 votes
Tags:
AlgorithmsApprovedDijkstra's algorithmFloyd Warshall AlgorithmGraphsMediumMinimum spanning treeOpen
Points:30
18 votes
Tags:
AlgorithmsApprovedGraphsGreedy AlgorithmsMediumMinimum spanning treeOpenSets