最小生成树除了prim还有kruskal算法。
Prim算法的实现,用到了优先队列。
仅供参考的c++代码:
#include <iostream>
#include <string>
#include <stack>
#include <fstream>
#include <sstream>
#include <ctime>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
/**********边表************/
class EdgeNode
{
public:
int adjvex;
int cost;
int head;
EdgeNode *next;
EdgeNode(int _head, int _adj, int _cost,EdgeNode *n = NULL) : head(_head), adjvex(_adj), cost(_cost), next(n) {}
};
/*=========顶点表============*/
class VertexNode
{
public:
int data;
EdgeNode *firstEdge;
VertexNode()
{
firstEdge = NULL;
}
};
/*********自定义比较函数(优先队列使用)************/
class mycompare
{
bool reverse;
public:
mycompare(const bool &re = true)
{
reverse = re;
}
bool operator() (EdgeNode *a, E