| 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
相关推荐
JieE2122 小时前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题JieE2122 小时前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解vivo互联网技术7 小时前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像Darling噜啦啦8 小时前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点用户4978630507311 小时前
(一)小红的数组操作怕浪猫14 小时前
Electron 系列文章封面图徐小夕16 小时前
JitWord 3.0 正式发布,高精度Word异构解析+复杂组件兼容,打造web端协同Word编辑器通信小呆呆1 天前
当算法有了“五感”:多模态数据融合如何向人体感官协同学习?