| 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 小时前
深入解析跳表SkipList:原理、实现与性能优化实战Jerry2 小时前
LeetCode 383. 赎金信ai产品老杨2 小时前
H264 H265视频分析常见问题和排查清单Jerry3 小时前
LeetCode 454. 四数相加 II可编程芯片开发3 小时前
基于CPS-SPWM链式STATCOM系统在电压不平衡环境下控制策略的simulink建模与仿真Jerry4 小时前
LeetCode 202. 快乐数hans汉斯4 小时前
基于改进交叉熵损失函数与Transformer的心电信号高风险分类研究Jerry4 小时前
LeetCode 349. 两个数组的交集YuK.W5 小时前
Leetcode100: 70.爬楼梯、118.杨辉三角、198.打家劫舍随意起个昵称5 小时前
状压dp-基础题目2([USACO12MAR] Cows in a Skyscraper G)