keymasterviriya1150

Prim’s MST Code

Apr 18th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. typedef pair<int, int> ii;
  2.  
  3. priority_queue<ii, vector<ii>, greater<ii>> pq;
  4.  
  5. vector<bool> taken;
  6.  
  7. taken.assign(V, false);
  8.  
  9. taken[0] = true;
  10.  
  11. mst_cost = 0;
  12.  
  13. for (auto it = edge[0].begin(); it != edge[0].end; it++)
  14.     pq.push(*it);
  15.  
  16. while (!pq.empty())
  17. {
  18.     ii front = pq.top(); pq.pop();
  19.  
  20.     u = front.second, w = front.first;
  21.  
  22.     if (!taken[u])
  23.     {
  24.         for (auto it = edge[u].begin(); it != edge[u].end(); it++)
  25.             pq.push(*it);
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment