zoukankan      html  css  js  c++  java
  • 算法总结——spfa(使用优先队列的Bellman_Ford算法)

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<functional>//greater 使得从小到大  ,less反
    //priority_queue<int, vector<int>, less<int> > que 相当于 priority_queue<int> que
    #include<queue>
    using namespace std;
    
    const int MAX = 100;
    const int inf = 0x3f3f3f3f;
    struct edge{
        int to, cost;
    }
    typedef pair<int, int> P;
    int V;
    vector<edge> G[MAX];
    int d[MAX];//距s的距离
    void dijkstra(int s)
    {
        priority_queue<P, vector<P>, greater<P> > que;
        fill(d, d + V, inf);
        d[s] = 0;
        que.push(P(0, s));
        while(!que.empty()){
            P p = que.top();
            que.pop();
            int v = p.second;
            if(d[v] < p.first) continue;//小优化
            for(int i = 0; i < G[v].size(); i++){
                edge e = G[v][i];
                if(d[e.to] > d[v] + e.cost){
                    d[e.to] = d[v] + e.cost;
                    que.push(P(d[e.to], e.to));
                }
            }
        }
    }
    

      

  • 相关阅读:
    Multidimensional Arrays
    TortoiseGit
    Excel教程(14)
    Excel教程(13)
    Excel教程(12)
    Excel教程(11)
    lock
    reflect
    game
    html x
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4658752.html
Copyright © 2011-2022 走看看