Dijkastra算法

1 Dijkstra(G,w,s)
2     initialize_single_source(G,s)
3     S is an empty container
4     Q=G.v
5     while Qis not empty
6         u=extract_min(Q)
7         S=S+u
8         for each vertex v in G.Ajd[u]
9             relax(u,v,w)

在C++語言的實現中,Q不是使用的優先隊列,而是使用list,便於從中刪除元素,可是每次定位元素都須要遍歷list,可是由於每次須要刪除元素,沒法經過索引肯定須要的元素,即便使用vector,仍然須要遍歷vector,因此使用list更具優點。G使用vector跟隨list同步更新,當list爲空是,G的更新完成。less

Q不能使用priority_queue是由於若是要定位某個元素,不便於遍歷隊列spa

補充幾點在今天變成中遇到的知識點:code

  • 兩條線段AB(a,b),CD(c,d),判斷兩條線段沒有重疊的部分(a>d||b<c)
  • <climits>

<climits> This header defines constants with the limits of fundamental integral types for the specific system and compiler implementation used. The limits for fundamental floating-point types are defined in <cfloat> (<float.h>). The limits for width-specific integral types and other typedef types are defined in <cstdint> (<stdint.h>).blog

  • greater&less,使用方法是priority_queue<int,greater<int> > que,或者sort(v.begin(),v.end(),greater<int>())
    std::greater
    Function object class for greater-than inequality comparison
    Binary function object class whose call returns whether its first argument compares greater than the second (as returned by operator >).
    
    Generically, function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a function call.
相關文章
相關標籤/搜索