Optimal control of water distribution systems by network flow theory