| 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 小时前
平口开槽箱 vs 飞机盒 vs 扣底盒:自动化、展示效果与成本核心区别AgentMaster1 小时前
从售前到售后全链路覆盖:智能客服在企业 5 大场景的落地实践与工具选型Cccp.1231 小时前
【leetcode】(六) 图和贪心算法y1su1 小时前
【Leetcode】1477. 找两个和为目标值且不重叠的子数组不会就选b9 小时前
算法日常・每日刷题--<贪心>14mmmmath_312 小时前
LeetCode.541.反转字符串IINavigator_Z12 小时前
LeetCode //MySQL - 1251. Average Selling Price醇氧13 小时前
MySQL 8.0 系统表损坏与引擎转换故障排查实战大熊背13 小时前
《Color constancy by characterization of illumination chromaticity》之色度色域最大化算法(二)