最短路径(C++版 Dijkstra即时版与延时版)

本文介绍了一种使用C++实现的Dijkstra算法,包括即时版和延时版两种变体,适用于寻找加权有向图中两点间的最短路径。文中详细展示了如何定义加权有向图的数据结构,并通过实例演示了算法的具体应用。

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

EdgeWeightedDigraph.h

#pragma once
#include <memory>
#include <fstream>
#include <stdexcept>
template <typename T>
class Dijkstra;
template <typename T>
class LazyDijkstra;
template <typename T>
class DijkstraSP;
template <typename T>
class LazyDijkstraSP;
template<typename T>
class EdgeWeightedDigraph
{
private:
    class AdjacentcyList
    {
    public:
        class DiEdge
        {
        public:
            std::shared_ptr<DiEdge> next;
            int v;
            int w;
            T weight;
        public:
            DiEdge(const int& s,const int& e,const T& t):v(s),w(e),weight(t),next(nullptr)
            {

            }
            DiEdge() = default;
            int from()
            {
                return v;
            }
            int to()
            {
                return w;
            }
            T Weight()
            {
                return weight;
            }
        };
        std::shared_ptr<DiEdge> head;
        class Iterator
        {
        private:
            std::shared_ptr<DiEdge> it;
        public:
            Iterator(std::shared_ptr<DiEdge> i) :it(i)
            {

            }
            bool operator == (const Iterator& rhs)const
            {
                return it == rhs.it;
            }
            bool operator != (const Iterator& rhs)const
            {
                return it != rhs.it;
            }
            Iterator operator ++()
            {
                it = it->next;
                return *this;
            }
            DiEdge operator *()const
            {
                if (it == nullptr)
                    throw std::out_of_range("* nullptr error");
                return *it;
            }
        };
        Iterator begin()const
        {
            return Iterator(head);
        }
        Iterator end()const
        {
            return Iterator(nullptr);
        }
        AdjacentcyList():head(nullptr)
        {

        }
/*****************************************
函数名称:   addDiEdge
******************************************/
        void addDiEdge(const int& s,const int& e,const T& t)
        {
            if (head == nullptr)
            {
                head = std::make_shared<DiEdge>(DiEdge(s, e, t));
                return;
            }
            std::shared_ptr<DiEdge> curr = head;
            while (curr->next != nullptr)
                curr = curr->next;
            curr->next = std::make_shared<DiEdge>(DiEdge(s, e, t));
            return;
        }
    };
private:
    std::unique_ptr<AdjacentcyList[]> adj;
    int nV;
    int nE;
public:
    EdgeWeightedDigraph(const std::string& file):nE(0)
    {
        std::ifstream in(file);
        in >> nV;
        adj = std::move(std::unique_ptr<AdjacentcyList[]>(new AdjacentcyList[nV]));
        while (!in.eof())
        {
            int pre, curr;
            T weight;
            in >> pre >> curr >> weight;
            adj[pre].addDiEdge(pre, curr, weight);
            ++nE;
        }
    }
    //friend class Dijkstra<T>;
    //friend class LazyDijkstra<T>;
    friend class DijkstraSP<T>;
    friend class LazyDijkstraSP<T>;
};

Dijkstra即时版算法 DijkstraSP.h

#pragma once
#include "EdgeWeightedDigraph.h"
#include "priority_queue.h"
#include <stack>

template <typename T>
class DijkstraSP
{
    using Ed = typename EdgeWeightedDigraph<T>::AdjacentcyList::DiEdge;
private:
    class Less
    {
    public:
        Less() { }
        bool operator()(const std::pair<int, T>& lhs, const std::pair<int, T>& rhs)
        {
            return lhs.second < rhs.second;
        }
    };
private:
    T spw;
    int s; //起点
    int e; //终点
    std::unique_ptr<Ed*[]> edge;
    std::unique_ptr<T[]> disTo;
    priority_queue<std::pair<int, T>,Less> pq;
private:
    void relax(EdgeWeightedDigraph<T>* ewd,const int& i)
    {
        for (auto &ed : ewd->adj[i])
        {
            int w = ed.to();
            if (disTo[w] > disTo[i] + ed.weight)
            {
                edge[w] = new Ed(i, w, ed.weight);
                disTo[w] = disTo[i] + ed.weight;
                pq.push(w, pair<int, T>(w, disTo[w]));
            }
        }
    }
public:
    DijkstraSP(EdgeWeightedDigraph<T>* ewd, const int& s, const int& e)
        :edge(new Ed*[ewd->nV]),disTo(new T[ewd->nV]),spw(),s(s),e(e)
    {
        for (int i = 0; i < ewd->nV; ++i)
        {
            edge[i] = nullptr;
            disTo[i] = std::numeric_limits<T>::max();
        }
        disTo[s] = T{};
        pq.push(s, pair<int, T>(s,disTo[s]));
        while (!pq.empty() && pq.top().first != e)
        {
            relax(ewd, pq.top().first);
            pq.pop();
        }
    }
    T SPW()
    {
        return disTo[e];
    }
    void path()
    {
        stack<Ed> sk;
        for (Ed* ed = edge[e]; ed != nullptr; ed = edge[ed->from()])
        {
            sk.push(*ed);
            if (s == ed->from())
                break;
        }
        while (!sk.empty())
        {
            cout << sk.top().from() << ends << sk.top().to() << ends << sk.top().weight <<endl;
            sk.pop();
        }
    }
};

LazyDijkstra 延时版算法 LazyDijkstraSP.h

#pragma once
#include "EdgeWeightedDigraph.h"
#include "priority_queue.h"
#include <stack>

template <typename T>
class LazyDijkstraSP
{
    using Ed = typename EdgeWeightedDigraph<T>::AdjacentcyList::DiEdge;
private:
    class Less
    {
    public:
        Less(){}
        bool operator()(const std::pair<T, Ed>& lhs, const std::pair<T, Ed>& rhs)
        {
            return lhs.first < rhs.first;
        }
    };
private:
    std::unique_ptr<Ed*[]> edge;
    std::unique_ptr<T[]> disTo;
    priority_queue<std::pair<T, Ed>,Less> pq;
    int s;
    int e;
private:
    void relax(EdgeWeightedDigraph<T>* ewd,const int& i)
    {
        for (auto ed : ewd->adj[i])
        {
            int w = ed.to();
            pq.push(std::pair<T, Ed>(disTo[w], Ed(i,w,ed.weight)));
            if (w == e)
                break;
        }
    }
public:
    LazyDijkstraSP(EdgeWeightedDigraph<T>* ewd,const int& s,const int& e)
        :edge(new Ed*[ewd->nV]),disTo(new T[ewd->nV]),s(s),e(e)
    {
        for (int i = 0; i < ewd->nV; ++i)
        {
            edge[i] = nullptr;
            disTo[i] = std::numeric_limits<T>::max();
        }
        disTo[s] = T{};
        relax(ewd,s);
        while (!pq.empty())
        {
            Ed ed = pq.top().second;
            int v = ed.from();
            int w = ed.to();
            pq.pop();
            if (disTo[w] > disTo[v] + ed.weight)
            {
                edge[w] = new Ed(v, w, ed.weight);
                disTo[w] = disTo[v] + ed.weight;
                relax(ewd, w);
            }
        }
    }
    void path()
    {
        std::stack<Ed> sk;
        for (Ed* ed = edge[e]; ed != nullptr; ed = edge[ed->from()])
        {
            sk.push(*ed);
            if (ed->from() == e)
                break;
        }
        while (!sk.empty())
        {
            cout << sk.top().from() << ends << sk.top().to() << ends << sk.top().weight << endl;
            sk.pop();
        }
    }
    T SPW()
    {
        return disTo[e];
    }
};

main.cpp

#include <iostream>
#include "EdgeWeightedDigraph.h"
#include "Dijkstra.h"
#include "LazyDijkstra.h"
#include "DijkstraSP.h"
#include "LazyDijkstraSP.h"
using namespace std;

int main()
{
    EdgeWeightedDigraph<double> ewd("test.txt");
    cout << "即时版Dijkstra: " << endl;
    DijkstraSP<double> dsp(&ewd, 0, 6);
    dsp.path();
    cout << dsp.SPW() << endl;
    cout << "延时版Dijkstra: " << endl;
    LazyDijkstraSP<double> ldsp(&ewd, 0, 6);
    ldsp.path();
    cout << ldsp.SPW();
    system("pause");
    return 0;
}

测试文件: test.txt

8
4 5 0.35
5 4 0.35
4 7 0.37
5 7 0.28
7 5 0.28
5 1 0.32
0 4 0.38
0 2 0.26
7 3 0.39
1 3 0.29
2 7 0.34
6 2 0.40
3 6 0.52
6 0 0.58
6 4 0.93

运行:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值