PRIM==>>MST模板
#include <iostream> using namespace std;
#define typec int
#define V 3
const typec inf=0x3f3f3f3f;
int vis[V];
typec lowc[V];
/*==================================================*\
| Prim求MST
| INIT: cost[][]耗费矩阵(inf为无穷大);
| CALL: prim(cost, n); 返回-1代表原图不连通;
\*==================================================*/
typec prim(typec cost[][V],int n)
{
int i,j,p;
typec minc,res=;
memset(vis,,sizeof(vis));
vis[]=;
for(i=; i<n; i++) lowc[i]=cost[][i];
for(i=; i<n; i++)
{
minc=inf;
p=-;
for(j=; j<n; j++)
{
if(==vis[j]&&minc>lowc[j])
{
minc=lowc[j];
p=j;
}
}
if(inf==minc) return -;
res+=minc;
vis[p]=;
for(j=; j<n; j++)
if(==vis[j]&&lowc[j]>cost[p][j])lowc[j]=cost[p][j];
}
return res;
} int main()
{
int cost[][]= {,,,,,,,,};
cout << prim(cost,) << endl;
return ;
}