| 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
相关推荐
2601_9622186114 分钟前
万象生鲜系统业财一体化底层打通技术自动生成经营账单吞下星星的少年·-·24 分钟前
The 2026 ICPC Asia East Continent Online Contest (II)(构造)stolentime2 小时前
洛谷P10515 转圈题解和裕3 小时前
蜂窝板 vs 七层瓦楞重型纸箱:大件工业设备运输性能与成本全对比衡石科技3 小时前
Data_Agent记忆机制与经验复用衡石分析智能体会话记忆与长期学习技术解析shirsl4 小时前
算法 Day 2 滑动窗口 + 栈 / 单调栈土司大王5 小时前
LeetCode hot100 回溯专题总结:Java 通用模板、决策树模型2601_962218615 小时前
万象生鲜系统订单全生命周期状态同步技术实现业务可视那年窗外下的雪.6 小时前
AIDC 学习日志|第 22 天|All-Active/Single-Active 故障演练设计wzdark7 小时前
基于启发式搜索的最优路径规划算法研究4