I would avoid the use of distanceSquared() when working out the shortest path. Imagine a right (90-degree) rectangle ABC (AB being the hypotenuse), and you are to find the shortest path from A to B.
Obviously, there are only two paths from A to B in total: one is A-B and the other is A-C-B. If you use distanceSquared(), you will see that both paths are the same (as it goes from the Pythagorean theorem). But it's obvious that the first path is the shortest.