priority_queue & 結構體||類 & 自定義比較函數cmp

 

 

 template< class T ,

                     class Sequence=vector<T> ,

                     class Compare=less<typename Sequence::value_type> >

 

 

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

struct node {
     int no;
     int year;
     int period;
     int current;
};
int num;

struct cmp {
     bool operator() (const node &a, const node &b)
     {
         if (a.current < b.current)
             return false;
         else if (a.current == b.current)
             return (a.year > b.year);
         else return true;
     }
};

priority_queue<node, vector<node>, cmp> p;
void slove()
{
     while (num--)
     {
         node e = p.top();
         p.pop();
         printf("%d/n", e.year);
         e.current += e.period;
         p.push(e);
     }
}

int main()
{
     node a;
     char s[100];

     for (int i=0; scanf("%s", s) && strcmp(s, "#"); i++)
     {
         scanf("%d%d", &a.year, &a.period);
         a.current = a.period;
         a.no = i;
         p.push(a);
     }
     scanf("%d", &num);
     slove();

     return 0;
}

 

 

 

 

 

priority_queue用法詳解v2

 

大部分內容來自某STL語法詳解文檔,貼出來應該沒問題吧~~

1.先給一個簡單應用的例子,這個和容器的用法差不多。

#include <iostream>
#include <queue>
using namespace std; 

int main()
{
    priority_queue<float> q;    //默認的是大頂堆 

    // insert three elements into the priority queue
    q.push(66.6);
    q.push(22.2);
    q.push(44.4); 

    // read and print two elements
    cout << q.top() << ' ';         //queue當中是q.front();
    q.pop();
    cout << q.top() << endl;
    q.pop(); 

    // insert three more elements
    q.push(11.1);
    q.push(55.5);
    q.push(33.3); 

    // skip one element
    q.pop(); 

    // pop and print remaining elements
    while (!q.empty())
    {
        cout << q.top() << ' ';
        q.pop();
    }
    cout << endl;
}

2.下面是將節點存在優先隊列中的兩種方式

最好的方式:這個簡潔!

struct Node
{
    int x,y;
    bool operator <(Node a) const  {  return y < a.y; }
    bool operator >(Node a) const  {  return y > a.y; }
};
    priority_queue<Node> A;                    //大根堆
    priority_queue<Node, vector<Node>, greater<Node> > B;    //小根堆 

方式一:

struct Node
{int adj;
 int val;
 friend  bool operator<(const Node &a,const Node &b) { return  a.val > b.val; }
};
priority_queue<Node>Q; 

方式二:(cmp將結構體以val由大到小排列,組成大根堆)一般也用這個!

struct Node
{int adj;
 int val;
};
struct cmp
{bool operator()(Node a,Node b) { return  a.val > b.val; }
};
priority_queue<Node,vector<Node>,cmp>Q; 

方式三:

struct TMax
{
    TMax(int tx):x(tx){}
    int x;
}; 

struct TMin
{
    TMin(int tx):x(tx){}
    int x;
}; 

bool operator<(const TMax &a, const TMax &b)
{
    return a.x<b.x;
} 

bool operator<(const TMin &a, const TMin &b)
{
    return a.x>b.x;
} 

priority_queue<TMax> hmax;    //大頂堆
priority_queue<TMin> hmin;    //小頂堆 

3.下面是將指針存在優先隊列中的方式

struct Node
{
     short f;
     short d;
     short fishs;
     short times;
     short i;
}; 

struct PCmp
{
    bool operator () (Node const *x, Node const *y)
    {
        if(x->f == y->f)
            return x->i > y->i;
        return x->f < y->f;
    }
}; 

priority_queue<Node*, vector<Node*>, PCmp > Q;

注:在這種情況下往往可以預分配空間以避免new帶來的麻煩。例如:堆中定義Node Qt[26], 棧中的使用則爲Node *tmp1 = Qt。

經過測試,在優選隊列當中存指針進行一系列操作要比存節點進行一系列操作快一些。

 

注:

  1. less<class T>這是大頂堆,按值大的優先,值大的在最上面。greater<class T>這是小頂堆,按值小的優先,值小的在最上面。
  2. 自定義cmp如果還有不明白的看這裏:
struct cmp
{
 bool operator()(const int &a,const int &b)//這裏的&是引用
 {
  return a>b;//最大堆
  return a<b;//最小堆
 }
};
priority_queue< int, vector<int>, cmp >
還是自定義cmp函數,注意,一般ACM中用結構體內含“bool operator()(const int &a,const int &b)”。這其實等價於Class cmp,不過更省事,當然也不規範(不需要規範)。 return就是希望如何排列爲true。如果希望由大到小,就將大到小的情況return;反則亦然。和sort的自定義cmp是一樣的。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章