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的更新完成。

Q不能使用priority_queue是因为如果要定位某个元素,不便于遍历队列

补充几点在今天变成中遇到的知识点:

  • 两条线段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>).

  • 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.

     

posted @ 2016-11-14 17:31  史昊  阅读(845)  评论(0编辑  收藏  举报