2020-10-17 //严蔚敏《数据结构》 //图:求最短路径Dijkstra算法

本文详细探讨了严蔚敏《数据结构》中关于图的最短路径问题,重点讲解了Dijkstra算法的应用。通过实例测试,阐述了如何利用该算法有效地找出图中两点间的最短路径。
摘要由CSDN通过智能技术生成
//严蔚敏《数据结构》
//图:求最短路径Dijkstra算法
//自学中,加油!
#include<iostream>
#include<string>
using namespace std;
#define InfoType double
#define VertexType string
const int MaxVertexNum=20;
typedef struct ArcBox
{
    int tailvex,headvex;//该弧的尾邻接点、头邻接点位置下标
    struct ArcBox* tlink,* hlink;//分别为弧尾相同、弧头相同的弧的链域
    InfoType* info;
}ArcBox;//弧

typedef struct VexNode
{
    VertexType data;
    ArcBox* firstin,* firstout;
}VexNode;

typedef struct
{
    VexNode xlist[MaxVertexNum];
    int vexnum,arcnum;
}OLGraph;

int Locate_xlist(OLGraph G,VertexType v)
{
    for(int i=0;i!=G.vexnum;i++){
        if(G.xlist[i].data==v)
            return i;
    }
    cout<<"未找到\n";
    return -1;
}

bool Creat_OLGraph(OLGraph& G)//用十字链表创建有向网
{
    cout<<"输入有向网的顶点数和弧数:";
    cin>>G.vexnum>>G.arcnum;
    cout<<"输入"<<G.vexnum<<"个顶点信息\n";
    for(int i=0;i!=G.vexnum;i++){//输入的同时对十字链表进行初始化
        cin>>G.xlist[i].data;
        G.xlist[i].firstin=G.xlist[i].firstout=nullptr;
    }
    VertexType v1,v2;
    InfoType w;
    int i,j;
    ArcBox* p;
    for(int k=0;k!=G.arcnum;k++){
        cout<<"输入两个顶点以及权值\n";
        cin>>v1>>v2>>w;
        i=Locate_xlist(G,v1);
        j=Locate_xlist(G,v2);
        p=new ArcBox;
        if(!p)
            return false;
        p->tailvex=i;p->headvex=j;
        p->info=new InfoType;
        *p->info=w;
        p->tlink=G.xlist[i].firstout;
        G.xlist[i].firstout=p;
        p->hlink=G.xlist[j].firstin;
        G.xlist[j].firstin=p;
    }
    return true;
}

void Output_OLGraph(OLGraph G)
{
    ArcBox* p;
    cout<<"各个顶点的出度\n";
    for(int i=0;i!=G.vexnum;i++){
        cout<<"顶点信息:"<<G.xlist[i].data;
        p=G.xlist[i].firstout;
        while(p){
            cout<<"以该顶点为弧尾的邻接点->"<<p->headvex;
            if(p->info)
                cout<<" 权值:"<<*p->info;
            p=p->tlink;
        }
        cout<<endl;
    }
    cout<<"------"<<endl;
    cout<<"各个顶点的入
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值