http://blog.csdn.net/hackerain/article/details/6041125
/*用普里姆算法求最小生成树*/
02.#include <iostream>
03.using namespace std;
04./*邻接矩阵的类型定义*/
05.#define MAX 10000000
06.#define MAX_VERTEX_NUM 20
07.typedef struct
08.{
09. char vexs[MAX_VERTEX_NUM];//用一维数组存储顶点信息
10. int edges[MAX_VERTEX_NUM][MAX_VERTEX_NUM];//用二维数组充当矩阵,来存储顶点边的信息
11. int vexnum,edgenum;//顶点树和边数
12.}MGraph;
13./*构造无向联通网的邻接矩阵*/
14.void CreateUDG_AM(MGraph &G,int n,int e)
15.{
16. G.vexnum=n;
17. G.edgenum=e;
18.
19. int i,j,k;
20. for(i=0;i<n;i++)
21. cin>>G.vexs[i];//输入顶点信息
22. for(i=0;i<n;i++)
23. for(j=0;j<n;j++)
24. G.edges[i][j]=MAX;//将矩阵初始化为无穷大
25. for(k=0;k<e;k++)
26. {
27. cin>>i>>j;
28. cin>>G.edges[i][j];//这里只用输入对称的边就行,也就是输入下矩阵或是上矩阵
29. G.edges[j][i]=G.edges[i][j];
30. }
31.}
32.//辅助数组定义
33.typedef struct
34.{
35. char adjvex;
36. int lowcost;
37.}CostOfVU;
38.CostOfVU closedge[MAX_VERTEX_NUM];//该一维数组用来存储V-U和U之间的权值
39.int LocateVex(MGraph &G,char x)
40.{//确定顶点在一维数组中的位置
41. int i=0;
42. while(G.vexs[i]!=x && i<G.vexnum)
43. i++;
44. return i;
45.}
46.int MinEdge(CostOfVU *p,MGraph &G)
47.{//求U和V-U之间最小的权值,在V-U中的顶点的位置
48. int i;
49. int k=0;
50. for(i=0;i<G.vexnum;i++)
51. {
52. while(p[k].lowcost==0)
53. k++;//找到权值不为0的顶点
54. while((p[i].lowcost==0||i==k)&&i<G.vexnum)
55. i++;//找到k后第一个不为0的顶点
56. if((p[k].lowcost>p[i].lowcost)&&i<G.vexnum)
57. k=i;//比较权值
58. }
59. return k;
60.}
61.//调用上面的函数求最小生成树
62.void MiniSpanTree_Prim(MGraph &G,char u)
63.{
64. int i,j,k;
65. k=LocateVex(G,u);//确定u在连通图中的位置
66. for(i=0;i<G.vexnum;i++)
67. {//将V-U中的顶点到U中的顶点的权值都存储到closedge数组中
68. closedge[i].adjvex=G.vexs[k];
69. closedge[i].lowcost=G.edges[k][i];
70. }
71. closedge[k].lowcost=0;//该句表示将顶点u纳入到U中
72. for(i=1;i<G.vexnum;i++)
73. {
74. k=MinEdge(closedge,G);//寻找closedge数组中权值最小的顶点,并返回其位置。
75. cout<<closedge[k].adjvex<<"-"<<G.vexs[k]<<":"<<closedge[k].lowcost<<endl;//输出找到的边和权值
76. closedge[k].lowcost=0;//将第k个顶点并入U集中
77. for(j=0;j<G.vexnum;j++)
78. {//更新V-U到U之间的权值
79. if(G.edges[k][j]<closedge[j].lowcost)
80. {
81. closedge[j].adjvex=G.vexs[k];
82. closedge[j].lowcost=G.edges[k][j];
83. }
84. }
85. }
86.}
87.void main()
88.{
89. freopen("a.txt","r",stdin);
90. //freopen("b.txt","w",stdout);
91. MGraph G;
92. CreateUDG_AM(G,6,9);
93. MiniSpanTree_Prim(G,'0');
94.}
#define MAX 10000000
#define MAX_VERTEX_NUM 20
typedef struct
{
char vexs[MAX_VERTEX_NUM];//一维数组存储顶点的信息
int edges[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
int vexnum,edgenum;//
}MGraph;
void CreateUDG_AM(MGraph &G,int n,int e)
{
G.vexnum=n;
G.edgenum=e;
int i,j,k;
for(i=0;i<n;i++)
cin>>G.vexs[i];//输入顶点的信息
for(i=0;i<n;i++)
for(j=0;j<n;j++)
G.edges[i][j]=MAX;
for(k=0;k<e;k++)
{
cin>>i>>j;
cin>>G.edges[i][j];
G.edges[j][i]=G.edges[i][j];
}
}
typedef struct
{
char adjvex;
int lowcost;
}CostOfVU;//辅助数组
CostOfVU closedge[MAX_VERTEX_NUM];
int LocateVex(MGraph &G,char x)
{
int i=0;
while(G.vexs[i]!=x&&i<G.vexnum)
i++;
return i;
}
int MinEdge(CostOfVU *p,MGraph &G)
{
int i;
int k=0;
for(i=0;i<G.vexnum;i++)
{
while(p[k].lowcost==0)
k++;
while((p[i].lowcost==0||i==k)&&i<G.vexnum)
{
i++;
}
if((p[k].lowcost>p[i].lowcost)&&i<G.vexnum)
{
k=i;
}
}
return k;
}
void MiniSpanTree_Prim(MGraph &G,char u)
{
int i,j,k;
k=LocateVex(G,u);
for(i=0;i<G.vexnum;i++)
{
closedge[i].adjvex=G.vexs[k];
closedge[i].lowcost=G.edges[k][i];
}
closedge[k].lowcost=0;
for(i=1;i<G.vexnum;i++)
{
k=MinEdge(closedge,G);
cout<<closedge[k].adjvex<<"-"<<G.vexs[k]<<":"<<closedge[k].lowcost<<endl;
closedge[k].lowcost=0;
for(j=0;j<G.vexnum;j++)
{
if(G.edges[k][j]<closedge[j].lowcost)
{
closedge[j].adjvex=G.vexs[k];
closedge[j].lowcost=G.edges[k][j];
}
}
}
}
int main()
{
MGraph G;
CreateUDG_AM(G,6,10);
MiniSpanTree_Prim(G,'a');
return 0;
}