Problems > Connecting Cities by Grid > Editorial

Connecting Cities by Grid — Solution & Editorial

Back to the Problem

Prim's algorithm on the complete Manhattan-distance graph is O(N²), sufficient here. (The specialized Manhattan-MST reduces the edge set to O(N) using 4-quadrant sweeps for N = 105.)

Complexity: O(N2)

Watch out: Total cost reaches ~109.