C++ Prim算法构造可以使n个城市连接的最小生成树

本文介绍使用Prim算法构建最小生成树的过程,并提供了一个具体的实现案例。通过邻接矩阵表示城市间距离网,实现了最小生成树的计算及展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:给定一个地区的n个城市间的距离网,用Prim算法建立最小生成树,并计算得到的最小生成树的代价。

基本要求:

1、城市间的距离网采用邻接矩阵表示,若两个城市之间不存在道路,则将相应边的权值设为自己定义的无穷大值。(要求至少10个城市,15条边)

2、最小生成树中包括的边及其权值,并显示得到的最小生成树的代价。


头文件.h

//
//  MGraph.h
//  Prim_XCode
//
//  Created by Holy-C on 14/12/22.
//  Copyright (c) 2014年 lyc. All rights reserved.
//

#ifndef MGraph_H
#define MGraph_H
#include <iostream>
#include <iomanip>
using namespace std;
#define MaxSize 30
#define INFINITYN 65536   //表示权值无限大

struct shortEdge                    //辅助数组
{
    int lowcost;
    int adjvex;
};

template <class DataType>
class MGraph
{
private:
    DataType vertex[MaxSize];       //存放顶点的数组
    int arcs[MaxSize][MaxSize];     //存放图中边的数组
    int versNum, arcsNum;           //定点数和边数
    shortEdge shortEdge[MaxSize];
    
public:
    MGraph();                       //初始化邻接矩阵
    ~MGraph(){}
    void CreateMGraph();
    void printMGraph();
    void Prim();                    //Prim算法生成最小生成树
};

template <class DataType>
MGraph<DataType>::MGraph()
{
    cout << "请输入顶点数和边数:" << endl;
    cin >> versNum >> arcsNum;
    cout << "请输入顶点字符信息(" << versNum << "个):" << endl;
    for (int i = 0; i < versNum; i++)
    {
        cin >> vertex[i];
    }
    for (int i = 0; i < versNum; i++)
    {
        for (int j = 0; j < versNum; j++)
        {
            if (i == j)
                arcs[i][j] = 0;
            else
                arcs[i][j] = INFINITYN;
        }
    }
}

template <class DataType>
void MGraph<DataType>::CreateMGraph()
{
    int i, j, w;
    cout << "请输入边<Vi,Vj>对应的顶点序号(" << arcsNum << "对),以及权值:" << endl;
    for (int k = 0; k < arcsNum; k++)
    {
        cin >> i >> j >> w;
        arcs[i][j] = w;
        arcs[j][i] = w;
    }
}

template <class DataType>
void MGraph<DataType>::printMGraph()
{
    cout << "邻接矩阵为:" << endl;
    for (int i = 0; i < versNum; i++)
    {
        for (int j = 0; j < versNum; j++)
        {
            if (arcs[i][j] == 65536)
                cout <<"  "<< setw(5) << "∞";
            else
                cout << setw(5) << arcs[i][j];
        }
        cout << endl;
        cout << endl;
    }
}

template <class DataType>
void MGraph<DataType>::Prim()
{
    int k, w, cost = 0;
    for (int i = 1; i < versNum; i++)
    {
        shortEdge[i].lowcost = arcs[0][i];
        shortEdge[i].adjvex = 0;
    }
    shortEdge[0].lowcost = 0;
    for (int i = 1; i < versNum; i++)
    {
        w = INFINITYN;
        for (int j = 1; j < versNum; j++)/* 在辅助数组closedge中选择权值最小的顶点*/
        {
            if (shortEdge[j].lowcost != 0 && shortEdge[j].lowcost < w)
            {
                w = shortEdge[j].lowcost;
                k = j;
            }    /* 求出生成树的下一个顶点k */
        }
        shortEdge[k].lowcost = 0;
        for (int j = 1; j < versNum; j++)
        {
            if (arcs[k][j] < shortEdge[j].lowcost) {
                shortEdge[j].lowcost = arcs[k][j];
                shortEdge[j].adjvex = k;
            }
        }
    }
    cout << "最小生成树为:" << endl;
    for (int i = 1; i < versNum; i++)
    {
        cout << i << "->" << shortEdge[i].adjvex << "," << arcs[i][shortEdge[i].adjvex] << endl;
        cost = cost + arcs[i][shortEdge[i].adjvex];
    }
    cout << "最小生成树代价为:" << cost << endl;
}
#endif

以下是测试函数:

//  
//  main.cpp  
//  Prim_XCode  
//  
//  Created by Holy-C on 14/12/22.  
//  Copyright (c) 2014年 lyc. All rights reserved.  
//  
  
#include "MGraph.h"  
#include <iostream>  
using namespace std;  
  
int main()  
{  
    MGraph<int> m1;  
    m1.CreateMGraph();  
    m1.printMGraph();  
    m1.Prim();  
}  


测试数据:


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值