Prim 求 MST| INIT: cost[][]耗费矩阵(inf为无穷大);

| Prim MST
| INIT: cost[][] 耗费矩阵 (inf 为无穷大 );
| CALL: prim(cost, n); 返回 -1 代表原图不连通 ;
\*==================================================*/
#define typec int // type of cost
const typec inf = 0x3f3f3f3f; // max of cost
int vis[V]; typec lowc[V];
typec prim(typec cost[][V], int n) // vertex: 0 ~ n-1
{
int i, j, p;
typec minc, res = 0;
memset(vis, 0, sizeof (vis));
vis[0] = 1;
for (i=1; i<n; i++) lowc[i] = cost[0][i];
for (i=1; i<n; i++) {
minc = inf; p = -1;
for (j=0; j<n; j++)
if (0 == vis[j] && minc > lowc[j]) {
minc = lowc[j]; p = j;
}
if (inf == minc) return -1; // 原图不连通
res += minc; vis[p] = 1;
for (j=0; j<n; j++)
if (0 == vis[j] && lowc[j] > cost[p][j])
lowc[j] = cost[p][j];
}
return res;
}

相关推荐
Allen_LVyingbo11 分钟前
医疗AI多智能体资源调度:用Python构建高性能MCU资源池
开发语言·人工智能·python·算法·知识图谱·健康医疗
叁散13 分钟前
实验项目3 温度传感器
人工智能·算法·机器学习
settingsun122514 分钟前
【AI-算法-02】卷积 Convolution
人工智能·算法
Hcoco_me17 分钟前
大模型面试题48:从白话到进阶详解LoRA 中 r 和 alpha 参数
开发语言·人工智能·深度学习·算法·transformer·word2vec
多米Domi01124 分钟前
0x3f 第24天 黑马web (安了半天程序 )hot100普通数组
数据结构·python·算法·leetcode
Swift社区24 分钟前
LeetCode 468 验证 IP 地址
tcp/ip·算法·leetcode
ytttr8739 小时前
隐马尔可夫模型(HMM)MATLAB实现范例
开发语言·算法·matlab
点云SLAM10 小时前
凸优化(Convex Optimization)理论(1)
人工智能·算法·slam·数学原理·凸优化·数值优化理论·机器人应用
jz_ddk10 小时前
[学习] 卫星导航的码相位与载波相位计算
学习·算法·gps·gnss·北斗
放荡不羁的野指针10 小时前
leetcode150题-动态规划
算法·动态规划