| 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
相关推荐
aaaameliaaa5 小时前
字符函数和字符串函数城管不管6 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别月疯7 小时前
二分法算法(水平等分图形面积)豆瓣鸡7 小时前
算法日记 - Day3白白白小纯7 小时前
算法篇—反转链表Achou.Wang7 小时前
深入理解go语言-第5章 并发编程——Go的灵魂The Chosen One9858 小时前
高进度算法模板速记(待完善)土豆.exe10 小时前
Fastjson2 2.0.53 哈希碰撞 RCE:从原理到三种打法黄河123长江10 小时前
有限Abel群的结构()Jerry11 小时前
LeetCode 92. 反转链表 II