Using prim algorithm to achieve minimum spanning tree in Java