无向图、有向图均适用,算法原理就不说了,百度讲的很明白的。
#include<iostream>
using namespace std;
#define INF 100000
//定义一个矩阵类并且重写复制构造函数、重载等号运算符
typedef class Matrix_Node
{
public:
int num;
int row;
int col;
Matrix_Node() :num(0), row(0), col(0)
{
}
Matrix_Node(Matrix_Node& M)
{
this->num = M.num;
this->row = M.row;
this->col = M.col;
}
Matrix_Node& operator=(const Matrix_Node& M)
{
this->num = M.num;
this->row = M.row;
this->col = M.col;
return *this;
}
}Matrix;
//普利姆算法类
class Prim
{
private:
int m_n;//结点个数
Matrix** Mat;;//权值矩阵
int* v1;//已选入点数组
int* v2;//未选入点数组
int* Pre;//各点的前驱点数组
int* Weight;//起始点到其他各点的权值数组
int N;//起始点
public:
Prim(int n)//构造函数,为了方便演示就没有写无输入参数的重载
{
m_n = n;
//输入权值矩阵
Mat = new Matrix