We consider the shape optimization problem \hbox{$\min\big\{\E(\Gamma)\ :\ \Gamma\in\A,\ \H^1(\Gamma)=l\\big\},$}min{ℰ(Γ):Γ ∈ 𝒜,ℋ1(Γ) = l}, where ℋ1 is the one-dimensional Hausdorffmeasure and𝒜is an admissible class of one-dimensional setsconnecting some prescribed set of points \hbox{$\D=\{D_1,\dots,D_k\}\subset\R^d$}𝒟 = { D1,...,Dk } ⊂ Rd. The cost functional ℰ(Γ) is theDirichlet energy of Γ defined through the Sobolev functions onΓ vanishing on the pointsDi. We analyze the existence of a solutionin both the families of connected sets and of metric graphs. At the end, several explicitexamples are discussed.