1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| #include "graph.hpp"
#include <list> #include <stdlib.h> #include <crtdbg.h> using namespace std;
class cmpEdge { public: bool operator()(const Edge *e1, const Edge *e2) const { return e1->weight > e2->weight; } };
unordered_set<Edge *> primMST(Graph *graph) { priority_queue<Edge *, vector<Edge *>, cmpEdge> pq; unordered_set<Node *> myset; unordered_set<Edge *> result;
for (auto &it : graph->nodes) { if (myset.find(it.second.get()) == myset.end()) { myset.insert(it.second.get()); for (auto &edge : it.second.get()->edges) { pq.push(edge); } while (!pq.empty()) { Edge *edge = pq.top(); pq.pop(); Node *toNode = edge->to; if (myset.find(toNode) == myset.end()) { result.insert(edge); for (auto &nextEdge : toNode->edges) { pq.push(nextEdge); } } } } } return result; }
void test1() { priority_queue<int, vector<int>, greater<int>> pq; pq.push(1); pq.push(1); pq.push(1); pq.push(1); while (!pq.empty()) { cout << pq.top() << endl; pq.pop(); } }
int main() { test1();
return 0; }
|