STL----priority_queue

            优先队列的常用知识

  queue:队头front,队尾back。插入push,删除pop

  pqueue:堆顶:top,插入push,删除pop

  1.是一个大根二叉堆,大根堆,大的在前

  2.que.push(x),q.pop()的时间复杂度为O(logn)

  3.如果优先队列想要储存结构体,需要对“<"运算符重载

    (1).在结构体外重载

      

struct node
{
    int id;
    int x,y;

}v[55];
priority_queue<node>que;
bool operator <(const node& a,const node& b){
    if(a.x == b.x) return a.y > b.y;
    return a.x > b.x;
}

    (2).结构体内重载

struct node
{
    int id;
    int x,y;
    bool operator < (const node& t)const{
        if( x == t.x) return y < t.y;  
        return x > t.x;
    }
}v[55];
priority_queue<node>que;

   4.priority_queue 实现小根堆

    (1)重载,和第三个标题差不多

    (2)对于int等内置数值类型,可以把插入的元素的相反数放入堆中

      当我写迪杰斯特拉优先队列优化时候,就喜欢插入相反数而实现小根堆,注意double细节不同,之前因为double类型也相反数插入wa了好多次!!

 

  

posted @ 2019-08-16 15:05  阿斯水生产线  阅读(123)  评论(0编辑  收藏  举报