| 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
相关推荐
vibecoding日记6 小时前
双非如何快速入职字节等大厂大模型?真实案例分析:推理优化和投机解码yszaygr21388 小时前
Verilog参数化游程编码RLE模块望易9 小时前
刚设计的大模型架构-双域耦合认知框架复杂网络13 小时前
多个 Claude Code 与多个 Codex 协同工作:设计与实现方案HjhIron1 天前
面试常客:字符串算法从入门到进阶吴佳浩1 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析触底反弹1 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战vivo互联网技术1 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾浮生望2 天前
JS字符串与回文算法:从包装类到双指针的面试进阶之路