趣味算法-城市之间最短总距离

城市之间最短总距离:图为联通图。


(1) 图中所有顶点集合为V, 最小生成树顶点集合为U初始时V包含所有顶点,U为空。

(2) 从V中选取一个顶点V0,将其加入U。

(3) 从V0的邻接顶点中选取边权值最小的Vn,得到最小生成树的一条边,将Vn加入集合U。

(4) 再从V-U中再选取一个与V0, Vn邻接的顶点,找出权值最小的边。

(5) 重复上述步骤。


#include <stdio.h>
#define MAX_DIST 65535

int calc(int arrGraph[5][5], int nPoints)
{
    int i = 0;
    int j = 0;
    int nDist   = MAX_DIST;
    int nPoint  = 0;
    int nResult = 0;
    int nLen = 0;

    // there are two sets for U and V, we will select point from U and set to V
    int *arrPointsU = (int*)malloc(nPoints*sizeof(int));
    int *arrPointsV = (int*)malloc(nPoints*sizeof(int));

    for (i = 0; i < nPoints; i++)
    {
        arrPointsU[i] = i;
        arrPointsV[i] = -1;
    }

    // start from point 0
    arrPointsV[0] = 0;
    arrPointsU[0] = -1;
    in
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值