Implement Prim's algorithm (lazy version)