| 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
相关推荐
圣光SG6 小时前
Java操作题练习(七)Cx330_FCQ7 小时前
Tmux使用拳里剑气8 小时前
C++算法:多源BFSysa0510309 小时前
【板子】短序列dp(换成维护更小常数维度的dp)shwill1239 小时前
PID 算法(三)--- 增量 PID ↔ 单神经元 PID 等价映射wabs66610 小时前
关于图论【卡码网110.字符串迁移的思考】hanlin0311 小时前
刷题笔记:力扣第242、349题(哈希表)浮沉98711 小时前
二分查找算法例题(二)only-qi12 小时前
大模型Agent面试攻略:落地工程痛点、评估体系与Agentic RAG核心精讲Gauss松鼠会14 小时前
【GaussDB】GaussDB锁阻塞源头查询