| Prim 求 MST
| INIT: cost\[\]\[\] 耗费矩阵 (inf 为无穷大 );
| CALL: prim(cost, n); 返回 -1 代表原图不连通 ;
\*==================================================*/
#define typec int // type of cost
const typec inf = 0x3f3f3f3f; // max of cost
int visV; typec lowcV;
typec prim(typec cost\[\]V, int n) // vertex: 0 ~ n-1
{
int i, j, p;
typec minc, res = 0;
memset(vis, 0, sizeof (vis));
vis0 = 1;
for (i=1; i<n; i++) lowci = cost0i;
for (i=1; i<n; i++) {
minc = inf; p = -1;
for (j=0; j<n; j++)
if (0 == visj && minc > lowcj) {
minc = lowcj; p = j;
}
if (inf == minc) return -1; // 原图不连通
res += minc; visp = 1;
for (j=0; j<n; j++)
if (0 == visj && lowcj > costpj)
lowcj = costpj;
}
return res;
}
Prim 求 MST| INIT: cost[][]耗费矩阵(inf为无穷大);
千秋TʌT2023-09-18 12:38
相关推荐
通信小呆呆3 小时前
当算法有了“五感”:多模态数据融合如何向人体感官协同学习?benben0443 小时前
强化学习之DQN算法族(基于gymnasium开发)何以解忧,唯有..4 小时前
Go语言循环语句详解:for、range与循环控制想吃火锅10055 小时前
【leetcode】88.合并两个有序数组js生成论实验室6 小时前
机器人:一个自主运动的系统Qres8216 小时前
算法复键——树状数组H178535090966 小时前
SolidWorks第四部分_直接实体建模特征9_替换面原理不会就选b6 小时前
算法日常・每日刷题--<二分查找>3绿算技术7 小时前
Mooncake 与绿算ForinnBase GroundPool如何联手打破推理僵局?