time complexity: O(E\,log\,E) = O(V^2\,log\,V) (which is more suitable for sparse graph)
As for O(V^2) implementation, just use array(random-access) and linear search to implement the min-heap
intprim(){// initializingfill(min_w,min_w+N,INF);fill(used,used+N,false);// first: weight of edge, second: id of vertexusingpii=pair<int,int>;// min heappriority_queue<pii,vector<pii>,greater<pii>>pq;pq.push(pii(0,SRC));min_w[SRC]=1;inttot=0;inttree_size=0;while(tree_size<N){if(pq.empty())returnNOT_CONNECTED;intv=pq.top().second;intw=pq.top().first;pq.pop();if(min_w[v]!=w)continue;tot+=w;used[v]=true;++tree_size;for(piiedge:G[v]){intto=edge.second;intwei=edge.first;if(!used[to]&&wei<min_w[to]){pq.push(pii(wei,to));min_w[to]=wei;}}}returntot;}