This paper investigates the problem of minimizing the global energy cost for multiagent systems to achieve consensus over undirected network topologies. Conventional optimization problems so defined require all-to-all network topologies for interagent communications, which precludes the use of distributed control or otherwise demands that the network be complete. To circumvent this difficulty, we introduce a network approximation (NA) scheme in the optimization criterion, which tends to render the optimization problem to one seemingly over a complete graph network, thus removing the all-to-all communication requirement. With the NA cost, we show that a distributed optimal consensus algorithm always exists for any given connected network topology, which can be determined by solving a single-agent-level parametric algebraic Ricatti equation (PARE). We also investigate the performance of the optimal consensus algorithm, focusing on the minimal energy cost required to achieve consensus optimally, and the speed at which consensus is achieved. Furthermore, for certain more special yet worthy cases, such as single-integrator, double-integrator, and first-order unstable agents, we derive explicit expressions for the energy cost and the consensus speed. It can be seen from these results that the energy cost can be made arbitrarily small for single-integrator and double-integrator systems under the optimal distributed control. On the other hand, for the first-order unstable agents, the energy cost increases and consensus speed decreases monotonically with the value of the agent's real unstable pole.