Prim 求 MST| INIT: cost[][]耗费矩阵(inf为无穷大);

| 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;
}

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