| 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
相关推荐
丑梨数据9 分钟前
信创环境里跑文档工具:格式兼容与本地合规的几个坑erzong2312 小时前
Linux操作系统:线程概念与控制Nil2082 小时前
leetcode 5最长回文子串xxwl5853 小时前
区域赛分类题解m0_674571973 小时前
力扣 hot 100 python青少儿编程课堂3 小时前
蓝桥杯+全国青少年信息素养大赛+电子学会PythonC/C++考级:最长公共子序列LCS解析倚楼听风语4 小时前
Kotlin 协程与 suspend 原理详解程序员清风4 小时前
双指针与滑动窗口:高效处理连续区间代钦塔拉5 小时前
【C++/Qt 进阶】设计模式系列(2):工厂模式——从游戏角色到披萨店思茂信息5 小时前
电磁仿真学涉及哪些算法?