This paper proposes a three-layer model to find the optimal routing of an underground electrical distribution system, employing the PRIM algorithm as a graph search heuristic. In the algorithm, the first layer handles transformer allocation and medium voltage network routing, the second layer deploys the low voltage network routing and transformer sizing, while the third presents a method to allocate distributed energy resources in an electric distribution system. The proposed algorithm routes an electrical distribution network in a georeferenced area, taking into account the characteristics of the terrain, such as streets or intersections, and scenarios without squared streets. Moreover, the algorithm copes with scalability characteristics, allowing the addition of loads with time. The model analysis discovers that the algorithm reaches a node connectivity of 100%, satisfies the planned distance constraints, and accomplishes the optimal solution of underground routing in a distribution electrical network applied in a georeferenced area. Simulating the electrical distribution network tests that the voltage drop is less than 2% in the farthest node.