We consider the shape optimization problem min{E(Γ):Γ∈A,H1(Γ)=l}, where H1 is the one-dimensional Hausdorff measure and A is an admissible class of one-dimensional sets connecting some prescribed set of points D={D1,…,Dk}⊂Rd. The cost functional E(Γ) is the Dirichlet energy of Γ defined through the Sobolev functions on Γ vanishing on the points Di. We analyze the existence of a solution in both the families of connected sets and of metric graphs. At the end, several explicit examples are discussed.