图的常见算法:最小生成树-prim算法
struct cmp1
{
bool operator()(Edge& a, Edge& b)
{
return a.weight > b.weight;
}
};
std::set<Edge*> primMST(Graph graph)
{
std::priority_queue<Edge*, std::vector<Edge*>, cmp1> graphqueue;
std::set<Node*> setnode;
std::set<Edge*> resset;
Node* cur = graph.nodes.at(0);
if (!setnode.count(cur))
{
setnode.insert(cur);
for (int i = 0; i < cur->edges.size(); i++)
{
graphqueue.push(cur->edges[i]);
}
while (!graphqueue.empty())
{
Edge* edge = graphqueue.top();
Node* to = edge->to;
if (!setnode.count(to))
{
resset.insert(edge);
setnode.insert(to);
for (int i = 0; i < to->edges.size(); i++)
{
graphqueue.push(to->edges[i]);
}
}
}
}
return resset;
}