城市之间最短总距离:图为联通图。
(1) 图中所有顶点集合为V, 最小生成树顶点集合为U初始时V包含所有顶点,U为空。
(2) 从V中选取一个顶点V0,将其加入U。
(3) 从V0的邻接顶点中选取边权值最小的Vn,得到最小生成树的一条边,将Vn加入集合U。
(4) 再从V-U中再选取一个与V0, Vn邻接的顶点,找出权值最小的边。
(5) 重复上述步骤。
#include <stdio.h>
#define MAX_DIST 65535
int calc(int arrGraph[5][5], int nPoints)
{
int i = 0;
int j = 0;
int nDist = MAX_DIST;
int nPoint = 0;
int nResult = 0;
int nLen = 0;
// there are two sets for U and V, we will select point from U and set to V
int *arrPointsU = (int*)malloc(nPoints*sizeof(int));
int *arrPointsV = (int*)malloc(nPoints*sizeof(int));
for (i = 0; i < nPoints; i++)
{
arrPointsU[i] = i;
arrPointsV[i] = -1;
}
// start from point 0
arrPointsV[0] = 0;
arrPointsU[0] = -1;
in