/*
* @Author: Achan
* @Date: 2019-03-30 23:03:36
* @Last Modified by: Achan
* @Last Modified time: 2019-03-30 23:09:04
*/
#include<bits/stdc++.h>
using namespace std;
struct node
{
int dis;
int nod;
node(){}
node(int dis,int nod):dis(dis),nod(nod){}
bool operator < (const node &a ) const{ //重载操作符'<'改为'>' 大根堆优先node队列变为小根堆
if(dis == a.dis) return nod > a.nod; //可定义多级优先度
return dis > a.dis;
}
};
priority_queue <int> p;//默认大根堆
priority_queue <node> q; //重载操作符实现小根堆
priority_queue <int, vector<int>, greater<int> > Q; //非重载操作符实现小根堆
int main(void)
{
cout<<"默认的大根堆优先队列"<<endl; //最大放在队首top()
node t;
p.push(1);
p.push(5);
p.push(9);
p.push(2);
p.push(6);
while(!p.empty())
{
Q.push(p.top());
cout<<p.top()<<endl;
p.pop();
}
cout<<"***************"<<endl;
while(!Q.empty())
{
cout<<Q.top()<<endl;
Q.pop();
}
cout<<"***************"<<endl;
cout<<"重载排序'<'逻辑的小根堆二级优先队列"<<endl;
t.dis = 1;
t.nod = 5;
q.push(t);
t.dis = 1;
t.nod = 1;
q.push(t);
t.dis = 9;
t.nod = 5;
q.push(t);
t.dis = 2;
t.nod = 5;
q.push(t);
t.dis = 6;
t.nod = 5;
q.push(t);
while(!q.empty())
{
cout<<q.top().dis<<" "<<q.top().nod<<endl;
q.pop();
}
cout<<"***************"<<endl;
}