int graph[arraysize][arraysize];
bool final[arraysize];
int d[arraysize];
int maxData =0x7fffffff;
int N; //结点个数
int prim(int src)
{
int sum = 0;
int minData;
int i,j;
int v;
memset(final,0,sizeof(final));
for(i=1;i<=N;++i) d[i]=graph[src][i];
d[src] = 0; //初始化源点的最短距离为0
final[src] = true;
for(i=1;i<N;++i)
{
minData = maxData;
for(j=1;j<=N;++j)
{
if(!final[j] && d[j]<minData)
{
v = j;
minData = d[j];
}
}
final[v] = true;
sum +=d[v];
for(j=1;j<=N;++j)
{
if(!final[j] && graph[v][j]<maxData && d[j]>graph[v][j]) //prim算法仅在此处与Dijkstra算法不同
{
d[j] = graph[v][j];
}
}
}
return sum;
}