| 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
相关推荐
AI科技星29 分钟前
基于全域数学公理体系求解三元约束极值题【乖乖数学】伊玛目的门徒8 小时前
试用leetcode之典中典 二数之和问题Jerry8 小时前
LeetCode 226. 翻转二叉树想做小南娘,发现自己是女生喵10 小时前
【无标题】Kx_Triumphs11 小时前
HDU4348 To the moon(主席树区间修改模板)旖-旎12 小时前
《LeetCode647 回文子串 || LeetCode 5 最长回文子串》轻颂呀13 小时前
约瑟夫环问题科技大视界14 小时前
投资AI项目,传统尽调不够用了——李章虎律师拆解算法、数据、算力三大雷区郝学胜-神的一滴15 小时前
算法实战:最小k个数——大顶堆的优雅解法