| 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 分钟前
技术分享︱SIMFORGE HSF® 网格加密与热点加速技术实践CV@CV36 分钟前
世界模型与智能体协同推理逻辑jiayoushijie-泽宣39 分钟前
SHIFT-PathFormer:专家轨迹蒸馏、连续安全优化与超大迷宫实验UIU1142 小时前
P1003 [NOIP 2011 提高组] 铺地毯爱读书的小胖2 小时前
LeetCode 88. 合并两个有序数组 C 解题分析垆边人似月.3 小时前
删边使每个连通块权值和为 targetxxwl5853 小时前
区域赛算法模版第四章---数据结构xxwl5854 小时前
第六章---计算几何Logic1015 小时前
C语言/数据结构二维数组题解:找出行列O个数均为偶数的格子——奇偶性预处理+二次遍历