• 算法-图论-最短路径-BellmanFord算法


    处理含有负权环的图

    #include <iostream>
    #include <vector>
    #include <stack>
    #include "Edge.h"
    #include "IndexMinHeap.h"
    
    using namespace std;
    
    template<typename Graph,typename Weight>
    class BellmanFord
    {
    private:
        Graph &G;
        int s;//
        Weight *distTo;//距离
        vector<Edge<Weight>*> from;//最短路径是谁
        bool hasNegativeCycle;
    
        bool detectNegetiveCycle(){
            for(int i=0;i<G.V();i++){
                typename Graph::adjIterator adj(G,i);
                for(Edge<Weight>* e = adj.begin();!adj.end();e = adj.next()){
                    if(!from[e->w()] || dist[e->v()] + e->wt() < distTo[e->w()]){
                        return true;
                        }
                    }
                }
            return false;
        }
    public:
        BellmanFord(Graph &graph,int s):G(graph){
            this->s = s;
            distTo = new Weight[G.V()];
            for(int i =0;i<G.V();i++){
                from.push_back(NULL);
            };
            //BellmanFord
            distTo[s] =Weight();
    
            for(int pass =1;pass<G.V();pass++){
                //松弛操作
                for(int i=0;i<G.V();i++){
                    typename Graph::adjIterator adj(G,i);
                    for(Edge<Weight>* e = adj.begin();!adj.end();e = adj.next()){
                        if(!from[e->w()] || dist[e->v()] +e->wt() < distTo[e->w()]){
                            distTo[e->w()] = distTo[e-v()] + e->wt();
                            from[e->w()] =e;
                        }
                    }
                }
            }
            hasNegativeCycle = detectNegetiveCycle();
        };
        ~BellmanFord(){
            delete[] distTo;
        };
        bool negetiveCycle(){
            return hasNegativeCycle;
        }
    
        Weight shortestPathTo(int w){
            assert(w>=0 && w<G.V());
            assert(!hasNegativeCycle);
            return distTo[w];
        }
        bool hasPathTo(int w){
            assert(w>=0 && w<G.V());
            return marked[w];
        }
        void shortestPath(int w,vector<Edge<Weight>> &vec){
            assert(w>=0 && w<G.V());
            assert(!hasNegativeCycle);
    
            stack<Edge<Weight>*> s;
            Edge<Weight> *e = from[w];
            while (e->v() != e->w())
            {
                s.push(e);
                e  = from[e->v()];
            }
            while (!s.empty())
            {
                e = s.top();
                vec.push_back(*e);
                s.pop();
            }
        }
    
        void showPath(int w){
            assert(w >0 && w<G.V());
            assert(!hasNegativeCycle);
            vector<Edge<Weight>> vec;
            shortestPath(w,vec);
            for(int i=0;i<vec.size();i++){
                count<<vec[i].v()<<" ->";
                if(i==vec.size()-1)
                    count<<vec[i].w()<<endl;
            }
        }
    };
  • 相关阅读:
    实用图片滑块,传送带,幻灯片效果【附源码】
    Canvas 示例:4种超炫的网站动画背景效果
    GOCR.js – 使用 JS 识别出图片中的文本
    Flexslider
    30款最好的 Bootstrap 3.0 免费主题和模板
    应用 CSS3 动画实现12种风格的通知提示
    Sequence.js 实现带有视差滚动特效的图片滑块
    使用QFuture类监控异步计算的结果
    Qt中的常用容器类(解释比较全面,有插图)
    QMetaEnum获取枚举元信息
  • 原文地址:https://www.cnblogs.com/Erick-L/p/12677953.html
Copyright © 2020-2023  润新知