Editorial
1. Candidate positions of the moved point
Suppose point is moved. Let be the set of the other points, and let be the point after the move.
Fix any spanning tree in an optimal solution. If is the set of vertices adjacent to , the part of its weight that depends on the position of is
The two sums are independent. A sum of absolute differences is minimized at a median, so an optimal can be chosen as the -coordinate of a point in , and an optimal can be chosen as the -coordinate of a point in .
Therefore, it is sufficient to try every combination of an -coordinate and a -coordinate appearing in . There are at most candidate positions. If , the answer is .
2. Keeping only one MST of the remaining points
Let be the complete Manhattan graph on , and let be any MST of . After adding a candidate point , there is one new edge from to every point of .
It is sufficient to keep only the edges of and these new edges. Consider an MST of the augmented graph that uses as few old edges outside as possible. Suppose it contains such an edge . On the path between the endpoints of in , every edge has weight at most the weight of . Removing from the chosen MST creates a cut, and the path in contains an edge crossing this cut. Replacing with does not increase the weight and decreases the number of old edges outside , which is a contradiction.
Thus, for each candidate position, we only need to run Kruskal's algorithm on a graph with vertices and edges.
3. Sorting the new edges in linear time
The edges of do not change while is fixed, so they are sorted once. Let the candidate position be . Partition the points of into four regions.
- , : the distance is , so the order is decreasing .
- , : the distance is , so the order is increasing .
- , : the distance is , so the order is increasing .
- , : the distance is , so the order is increasing .
Pre-sort the points by these four keys. For one candidate position, filtering each order by its corresponding region produces four sorted streams of edges incident to . Merge these streams together with the sorted edges of while running Kruskal's algorithm. This takes time per candidate.
4. Complete algorithm
For every choice of the moved point :
- Build an MST of the complete Manhattan graph on using Prim's algorithm.
- Try every combination of a distinct -coordinate and a distinct -coordinate appearing in .
- For each candidate, merge the edges of and the four sorted streams of edges incident to , and run Kruskal's algorithm.
The first observation guarantees that some optimal moved position is considered. The second observation guarantees that the MST weight for every considered position is computed exactly. Since every original point is considered as the moved point, the minimum found by the algorithm is the answer.
There are candidate positions for each moved point, and each candidate is processed in time. Over all choices of the moved point, the time complexity is and the memory complexity is .
Solution written by GPT5.6