Motivated by the importance and challenges of the energy saving problem of parallel-connected pumps in heating, ventilation, and air-conditioning (HVAC) systems, we propose a distributed optimal control algorithm for on-off status and flow rate set points of parallel-connected pumps in HVAC systems. The proposed algorithm consists of two parts: First, in order to process the network information, we apply the breadth first search algorithm to construct a tree for exchanging messages. Second, all nodes coordinate with each other and randomly sample the speed ratios. To our best knowledge, the algorithm proposed in this paper is the first effort to address the challenges of existing studies at the same time. The algorithm solves the pump optimization problem in a distributed manner, achieves the minimum pump energy consumption and has the convergence guarantee. Even if some of the pumps break down, the whole system can still be working and have great flexibility. Simulation experiments on six parallel-connected pumps are provided for different working cases to demonstrate the effectiveness of the proposed algorithm and compare with the other four methods. The results show that our algorithm strictly satisfies the demand constraints and presents good energy saving potential, the convergence guarantee, and flexibility. The maximum energy saving can be up to 29.92%.