Redid Prim's algorithm