優先隊列:顧名思義,首先它是一個隊列,但是它強調了“優先”二字,所以,已經不能算是一般意義上的隊列了,它的“優先”意指取隊首元素時有一定的選擇性,即根據元素的屬性選擇某一項值最優的出隊~
百度百科上這樣描述的:
優先級隊列 是不同於先進先出隊列的另一種隊列。每次從隊列中取出的是具有最高優先權的元素
優先隊列的類定義
優先隊列是0個或多個元素的集合,每個元素都有一個優先權或值,對優先隊列執行的操作有1) 查找;2) 插入一個新元素;3) 刪除.在最小優先隊列(min priorityq u e u e)中,查找操作用來搜索優先權最小的元素,刪除操作用來刪除該元素;對於最大優先隊列(max priority queue),查找操作用來搜索優先權最大的元素,刪除操作用來刪除該元素.優先權隊列中的元素可以有相同的優先權,查找與刪除操作可根據任意優先權進行.
優先隊列,其構造及具體實現可以先不用深究,現在只需要了解其特性,及在做題中的用法,看過之後會收獲不少。
使用優先隊列,首先要包函STL頭文件 #include <queue>
以一個例子來解釋吧(呃,寫完才發現,這個代碼包函了幾乎所有我們要用到的用法,仔細看看吧):
1 /*優先隊列的基本使用 2010/7/24 dooder*/
2 #include<stdio.h>
3 #include<functional>
4 #include<queue>
5 #include<vector>
6 using namespace std;
7 //定義結構,使用運算符重載,自定義優先級1
8 struct cmp1{
9 bool operator ()(int &a,int &b){
10 return a>b;//最小值優先
11 }
12 };
13 struct cmp2{
14 bool operator ()(int &a,int &b){
15 return a<b;//最大值優先
16 }
17 };
18 //定義結構,使用運算符重載,自定義優先級2
19 struct number1{
20 int x;
21 bool operator < (const number1 &a) const {
22 return x>a.x;//最小值優先
23 }
24 };
25 struct number2{
26 int x;
27 bool operator < (const number2 &a) const {
28 return x<a.x;//最大值優先
29 }
30 };
31 int a[]={14,10,56,7,83,22,36,91,3,47,72,0};
32 number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0};
33 number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0};
34
35 int main()
36 { priority_queue<int>que;//采用默認優先級構造隊列
37
38 priority_queue<int,vector<int>,cmp1>que1;//最小值優先
39 priority_queue<int,vector<int>,cmp2>que2;//最大值優先
40
41 priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”會被認為錯誤,
42 //這是右移運算符,所以這裡用空格號隔開
43 priority_queue<int,vector<int>,less<int> >que4;////最大值優先
44
45 priority_queue<number1>que5;
46 priority_queue<number2>que6;
47
48 int i;
49 for(i=0;a[i];i++){
50 que.push(a[i]);
51 que1.push(a[i]);
52 que2.push(a[i]);
53 que3.push(a[i]);
54 que4.push(a[i]);
55 }
56 for(i=0;num1[i].x;i++)
57 que5.push(num1[i]);
58 for(i=0;num2[i].x;i++)
59 que6.push(num2[i]);
60
61
62 printf("采用默認優先關系:\n(priority_queue<int>que;)\n");
63 printf("Queue 0:\n");
64 while(!que.empty()){
65 printf("%3d",que.top());
66 que.pop();
67 }
68 puts("");
69 puts("");
70
71 printf("采用結構體自定義優先級方式一:\n(priority_queue<int,vector<int>,cmp>que;)\n");
72 printf("Queue 1:\n");
73 while(!que1.empty()){
74 printf("%3d",que1.top());
75 que1.pop();
76 }
77 puts("");
78 printf("Queue 2:\n");
79 while(!que2.empty()){
80 printf("%3d",que2.top());
81 que2.pop();
82 }
83 puts("");
84 puts("");
85 printf("采用頭文件\"functional\"內定義優先級:\n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)\n");
86 printf("Queue 3:\n");
87 while(!que3.empty()){
88 printf("%3d",que3.top());
89 que3.pop();
90 }
91 puts("");
92 printf("Queue 4:\n");
93 while(!que4.empty()){
94 printf("%3d",que4.top());
95 que4.pop();
96 }
97 puts("");
98 puts("");
99 printf("采用結構體自定義優先級方式二:\n(priority_queue<number>que)\n");
100 printf("Queue 5:\n");
101 while(!que5.empty()){
102 printf("%3d",que5.top());
103 que5.pop();
104 }
105 puts("");
106 printf("Queue 6:\n");
107 while(!que6.empty()){
108 printf("%3d",que6.top());
109 que6.pop();
110 }
111 puts("");
112 return 0;
113 }
114 /*
115 運行結果 :
116 采用默認優先關系:
117 (priority_queue<int>que;)
118 Queue 0:
119 91 83 72 56 47 36 22 14 10 7 3
120
121 采用結構體自定義優先級方式一:
122 (priority_queue<int,vector<int>,cmp>que;)
123 Queue 1:
124 3 7 10 14 22 36 47 56 72 83 91
125 Queue 2:
126 91 83 72 56 47 36 22 14 10 7 3
127
128 采用頭文件"functional"內定義優先級:
129 (priority_queue<int,vector<int>,greater<int>/less<int> >que;)
130 Queue 3:
131 3 7 10 14 22 36 47 56 72 83 91
132 Queue 4:
133 91 83 72 56 47 36 22 14 10 7 3
134
135 采用結構體自定義優先級方式二:
136 (priority_queue<number>que)
137 Queue 5:
138 3 7 10 14 22 36 47 56 72 83 91
139 Queue 6:
140 91 83 72 56 47 36 22 14 10 7 3
141 */
好了,如果你仔細看完了上面的代碼,那麼你就可以基本使用優先隊列了,下面給出一些我做題中有過的一些應用,希望能給大家帶來一些啟
示~
下面給出本文出處:http://www.cnblogs.com/heqinghui/archive/2013/07/30/3225407.html
附原作者部分代碼↓↓↓
1、先來一個我們最近做的題吧,http://acm.hdu.edu.cn/showproblem.php?pid=1242
題意:某人被關在囚籠裡等待朋友解救,問能否解救成功,最少需要多少時間~
具體:可同時有幾個朋友,每走一格消耗一分鐘的時間 ,地圖上還存在著衛兵,衛兵可以解決掉,但是要另外花費一分鐘~
分析:從“a”出發,此題可以用回溯法進行深搜,但那樣做的話,效率還是不能讓人滿意,但是廣搜的話,由於入隊後每次出隊時,根據地
圖情況的不同,出隊元素所記憶的時間並不是層次遞增的,因此使用簡單廣搜的話,同樣需要全部搜索才能找到正確答案。有沒有一種方法能
讓某一步因為遇到士兵而多花時間的結點在隊列中向後推遲一層出隊呢?答案是肯定的,在這裡我們可以用優先隊列來實現,總體思想上是,
根據時間進行優先性選擇,每次都要出隊當前隊列元素中記錄時間最少的出隊,而入隊處理時,我們可以按順序對四個方向上的各種情況按正
常處理入隊就行了,出隊順序由優先隊列根據預設優先性自動控制。這樣,我們就可以從“a”進行基於優先隊列的范圍搜索了,並且在第一
次抵達有朋友的位置時得到正確結果~具體實現代碼:
1 /*HDU 1242 基於優先隊列的范圍搜索,16ms dooder*/
2
3 #include<stdio.h>
4 #include<queue>
5 using namespace std;
6
7 #define M 201
8 typedef struct p{
9 int x,y,t;
10 bool operator < (const p &a)const
11 {
12 return t>a.t;//取時間最少優先
13 }
14 }Point;
15
16 char map[M][M];
17 Point start;
18 int n,m;
19 int dir[][2]={{1,0},{-1,0},{0,1},{0,-1}};
20
21 int bfs()
22 {
23 priority_queue<Point>que;
24 Point cur,next;
25 int i;
26
27 map[start.x][start.y]='#';
28 que.push(start);
29 while(!que.empty()){
30 cur=que.top();//由優先隊列自動完成出隊時間最少的元素
31 que.pop();
32 for(i=0;i<4;i++){
33 next.x=cur.x+dir[i][0];
34 next.y=cur.y+dir[i][1];
35 next.t=cur.t+1;
36 if(next.x<0||next.x>=n||next.y<0||next.y>=m)
37 continue;
38 if(map[next.x][next.y]=='#')
39 continue;
40 if(map[next.x][next.y]=='r')
41 return next.t;
42 if(map[next.x][next.y]=='.'){
43 map[next.x][next.y]='#';
44 que.push(next);
45 }
46 else if(map[next.x][next.y]=='x'){
47 map[next.x][next.y]='#';
48 next.t++;
49 que.push(next);
50 }
51 }
52 }
53 return -1;
54 }
55 int main()
56 {
57 int i,ans;
58 char *p;
59 while(scanf("%d%d",&n,&m)!=-1){
60 for(i=0;i<n;i++){
61 scanf("%s",map[i]);
62 if(p=strchr(map[i],'a')){
63 start.x=i;
64 start.y=p-map[i];
65 start.t=0;
66 }
67 }
68 ans=bfs();
69 printf(ans+1?"%d\n":"Poor ANGEL has to stay in the prison all his life.\n",ans);
70 }
71 return 0;
72 }
2、http://acm.hdu.edu.cn/showproblem.php?pid=1053
題意:給出一行字符串,求出其原編碼需要的編碼長度和哈夫曼編碼所需的長度,並求其比值
分析:根據哈夫曼生成樹的生成過程可知,其生成樹的權值是固定的而且這個值是最小的,而且其值根據生成樹的順序,我們可以找出規律而
不需要真的去生成一棵樹然後再求出權值,其模擬過程為取出隊列中權值最小的兩個元素,將其值加入結果中,然後將這兩個元素的權值求和
即得出其父節點的權值,將生成元素作為結點入隊~~如此循環,直至取出隊列中最後兩個元素加入結果,實現代碼如下:
1 /*HDU 1053 采用廣搜求哈夫曼生成樹的權值 0ms dooder*/
2 #include<stdio.h>
3 #include<string.h>
4 #include<ctype.h>
5 #include<functional>
6 #include<queue>
7 using namespace std;
8 #define M 1000050
9 char str[M];
10 int list[27];
11
12 priority_queue< int,vector<int>,greater<int> >que;
13
14 int main()
15 {
16 int ans,sum;
17 int i,a,b,c;
18 while(scanf("%s",str),strcmp(str,"END")){
19 memset(list,0,sizeof(list));
20 for(i=0;str[i];i++){
21 if(isalpha(str[i]))
22 list[str[i]-'A']++;
23 else
24 list[26]++;
25 }
26 sum=i*8;ans=i;c=0;
27 for(i=0;i<27;i++){
28 if(list[i]){
29 que.push(list[i]);
30 c++;
31 }
32 }
33 if(c>1){ans=0;//注意只有一種字符的情況
34 while(que.size()!=1){
35 a=que.top();
36 que.pop();
37 b=que.top();
38 que.pop();
39 ans+=a+b;
40 que.push(a+b);
41 }
42 while(!que.empty())//使用後清空隊列
43 que.pop();
44 }
45 printf("%d %d %.1f\n",sum,ans,1.0*sum/ans);
46 }
47 return 0;
48 }
3、http://acm.pku.edu.cn/JudgeOnline/problem?id=2263
這是第二次練習賽時,我們做過的最後一題,這裡采用優先隊列進行實現,在《誰說不能這樣做題》中已提到這種方法,在這裡再次放出代
碼,~
題意:給出各城市間道路的限制載重量,求出從一個城市到另外一個城市的貸車能夠運載的最大貨物重量。
分析:采用優先隊列,每次取出當前隊列中結點的minheavy最大值出隊,對它的連接結點搜索入隊,這樣,從出發點開始就可以
在到達終點時求出結果,即最大載貨物重,實現代碼如下:
1 /*POJ 2263 16ms dooder*/
2
3 #include<stdio.h>
4 #include<string.h>
5 #include<queue>
6 using namespace std;
7 #define M 201
8 typedef struct w{
9 int city;
10 int mintons;
11 bool operator < (const w &a)const {
12 return mintons < a.mintons;
13 }//優先性定義
14 }Way;
15 char citys[M][31];
16 int map[M][M];
17 bool mark[M][M];
18 int n,m,from,to,ans,k;
19 priority_queue <Way> que;
20 int min(int a,int b)
21 {
22 return a>b?b:a;
23 }
24 void bfs()
25 {
26 Way cur,next;
27 int i;
28 while(!que.empty()){
29 cur=que.top();
30 que.pop();
31 if(cur.city==to){
32 if(cur.mintons>ans)
33 ans=cur.mintons;
34 while(!que.empty())
35 que.pop();
36 return ;
37 }
38 for(i=0;i<n;i++){
39 if(map[cur.city][i]&&!mark[cur.city][i]){
40 next.city=i;
41 next.mintons=min(cur.mintons,map[cur.city][i]);
42
43 mark[cur.city][i]=mark[i][cur.city]=1;
44 que.push(next);
45 }
46 }
47 }
48 }
49 void run()
50 {
51 int i,temp,index;
52 Way cur;
53 ans=0;
54 memset(mark,0,sizeof(mark));
55 temp=0;
56 for(i=0;i<n;i++){
57 if(map[from][i]>temp){
58 temp=map[from][i];
59 index=i;
60 }
61 }
62 cur.city=index;
63 cur.mintons=temp;
64 que.push(cur);
65 bfs();
66 }
67 int main()
68 {
69 int k1,k2,tons,t=1;
70 char s1[31],s2[31];
71 while(scanf("%d%d",&n,&m),n||m){
72 k=0;
73 while(m--){
74 scanf("%s%s%d",s1,s2,&tons);
75 for(k1=0;strcmp(s1,citys[k1])&&k1<k;k1++);
76 if(k1==k)
77 strcpy(citys[k++],s1);
78 for(k2=0;strcmp(s2,citys[k2])&&k2<k;k2++);
79 if(k2==k)
80 strcpy(citys[k++],s2);
81 map[k1][k2]=map[k2][k1]=tons;
82 }
83 scanf("%s%s",s1,s2);
84 for(from=0;strcmp(citys[from],s1);from++);
85 for(to=0;strcmp(citys[to],s2);to++);
86 run();
87 printf("Scenario #%d\n",t++);
88 printf("%d tons\n\n",ans);
89 }
90 return 0;
91 }
當然了,優先隊列的用法決不是僅僅提到的這些,各種應用還需要大家去發現,給道題大家可以練習一下hdu 2066\
相信大家已經學到不少了,還有一點可以告訴大家,優先隊列是啟發式搜索的數據結構基礎,希望好好理解,並逐步掌握其用法~
加:失策啊,竟然忘了說優先隊列的效率了,其時間復雜度為O(logn).n為隊列中元素的個數,存取都需要消耗時間~