include[HTML_REMOVED]
include[HTML_REMOVED]
using namespace std;
define MaxVex 100
typedef char VexType;
typedef int EdgeType;
typedef struct{
VexType node[MaxVex];
EdgeType edges[MaxVex][MaxVex];
int vexnum,edgenum;
}AdjMatrix;
//邻接矩阵定义
typedef struct {
int v0;
int v1;
int weight;
}Edge;
define MaxInt 32
typedef struct{
int treenode;
int weight;
}join;
void MST_prim(AdjMatrix G,int n){
join joinTree[MaxVex];//这里可以吧数组简单定义dist,后面会好写很多
for(int i=1;i<=G.vexnum;i++){
joinTree[i].weight=G.edges[n][i];
if(joinTree[i].weight[HTML_REMOVED]G.edges[v0][j]){
joinTree[j].weight=G.edges[v0][j];
joinTree[j].treenode=v0;
}
}
}
}
void create(AdjMatrix &G,Edge data[],int n){
for(int i=1;i<=MaxVex-1;i){
for(int j=1;j<=MaxVex-1;j)
G.edges[i][j]=MaxInt;
}
for(int i=1;i<=G.vexnum;i) G.edges[i][i]=0;
for(int i=0;i<n;i){
G.edges[data[i].v0][data[i].v1]=data[i].weight;
G.edges[data[i].v1][data[i].v0]=data[i].weight;
}
}
int main(){
AdjMatrix G;
G.vexnum=7;
G.edgenum=12;
Edge data[]={{1,2,23},{1,7,36},{1,6,28},{2,7,1},{2,3,20},
{3,7,4},{3,4,15},{4,7,9},{5,4,3},{5,7,16},{5,6,17},{6,7,25}
};
AdjMatrix &M=G;
create(M,data,12);
for(int i=1;i<=G.vexnum;i){
for(int j=1;j<=G.vexnum;j)
cout<<setw(3)<<G.edges[i][j]<<’ ‘;
cout<<endl;
}
MST_prim(G,5);
}
//最小生成树的图https://blog.csdn.net/qq_62213124/article/details/121597780?ops_request_misc=%257B%2522request%255Fid%2522%253A%25223A3D4474-A160-4B6B-946A-E877C25C144F%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=3A3D4474-A160-4B6B-946A-E877C25C144F&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-121597780-null-null.142^v100^pc_search_result_base5&utm_term=prim&spm=1018.2226.3001.4187