| 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
相关推荐
旖旎夜光1 小时前
面试题 01.02: 判定是否互为字符重排 (哈希表) —— 题解Navigator_Z3 小时前
LeetCode //C - 1326. Minimum Number of Taps to Open to Water a Gardengeats人山人海14 小时前
算法基础第六讲 图论风落青芜17 小时前
排序算法(四):归并排序、计数排序与算法总结@陈小鱼17 小时前
基于InceptionTime网络的PPG无创血压估计算法研究liulilittle1 天前
认识麻将 —— 流水及对账郝学胜-神的一滴1 天前
Effective Python 条款44:善用属性注解青少儿编程课堂2 天前
第十八届蓝桥杯青少组图形化真题:校园分形雪花绘制解析(全国青少年信息素养大赛·电子学会图形化考级冲刺)