博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
优先队列 如何使用
阅读量:6966 次
发布时间:2019-06-27

本文共 8725 字,大约阅读时间需要 29 分钟。

原文地址:http://blog.chinaunix.net/uid-21712186-id-1818266.html
优先队列:顾名思义,首先它是一个队列,但是它强调了“优先”二字,所以,已经不能算是一般意义上的队列了,它的“优先”意指取队首元素时,有一定的选择性,即根据元素的属性选择某一项值最优的出队~
百度百科上这样描述的:
  优先级队列 是不同于先进先出队列的另一种队列。每次从队列中取出的是具有最高优先权的元素
  优先队列的类定义  
   优先队列是0个或多个元素的集合,每个元素都有一个优先权或值,对优先队列执行的操作有1) 查找;2) 插入一个新元素;3) 删除.在最小优先队列(min priorityq u e u e)中,查找操作用来搜索优先权最小的元素,删除操作用来删除该元素;对于最大优先队列(max priority queue),查找操作用来搜索优先权最大的元素,删除操作用来删除该元素.优先权队列中的元素可以有相同的优先权,查找与删除操作可根据任意优先权进 行. 
优先队列,其构造及具体实现我们可以先不用深究,我们现在只需要了解其特性,及在做题中的用法,相信,看过之后你会收获不少。
使用优先队列,首先要包函STL头文件"queue",
以一个例子来解释吧(呃,写完才发现,这个代码包函了几乎所有我们要用到的用法,仔细看看吧):
view plaincopy to clipboardprint?
/*优先队列的基本使用    2010/7/24    dooder*/  
1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 //定义结构,使用运算符重载,自定义优先级1 7 struct cmp1{ 8 bool operator ()(int &a,int &b){ 9 return a>b;//最小值优先 10 } 11 }; 12 struct cmp2{ 13 bool operator ()(int &a,int &b){ 14 return a
a.x;//最小值优先 22 } 23 }; 24 struct number2{ 25 int x; 26 bool operator < (const number2 &a) const { 27 return x
que;//采用默认优先级构造队列 36 37 priority_queue
,cmp1>que1;//最小值优先 38 priority_queue
,cmp2>que2;//最大值优先 39 40 priority_queue
,greater
>que3;//注意“>>”会被认为错误, 41 //这是右移运算符,所以这里用空格号隔开 42 priority_queue
,less
>que4;////最大值优先 43 44 priority_queue
que5; 45 priority_queue
que6; 46 47 int i; 48 for(i=0;a[i];i++){ 49 que.push(a[i]); 50 que1.push(a[i]); 51 que2.push(a[i]); 52 que3.push(a[i]); 53 que4.push(a[i]); 54 } 55 for(i=0;num1[i].x;i++) 56 que5.push(num1[i]); 57 for(i=0;num2[i].x;i++) 58 que6.push(num2[i]); 59 60 61 printf("采用默认优先关系:\n(priority_queue
que;)\n"); 62 printf("Queue 0:\n"); 63 while(!que.empty()){ 64 printf("%3d",que.top()); 65 que.pop(); 66 } 67 puts(""); 68 puts(""); 69 70 printf("采用结构体自定义优先级方式一:\n(priority_queue
,cmp>que;)\n"); 71 printf("Queue 1:\n"); 72 while(!que1.empty()){ 73 printf("%3d",que1.top()); 74 que1.pop(); 75 } 76 puts(""); 77 printf("Queue 2:\n"); 78 while(!que2.empty()){ 79 printf("%3d",que2.top()); 80 que2.pop(); 81 } 82 puts(""); 83 puts(""); 84 printf("采用头文件\"functional\"内定义优先级: \n(priority_queue
,greater
/less
>que;)\n"); 85 printf("Queue 3:\n"); 86 while(!que3.empty()){ 87 printf("%3d",que3.top()); 88 que3.pop(); 89 } 90 puts(""); 91 printf("Queue 4:\n"); 92 while(!que4.empty()){ 93 printf("%3d",que4.top()); 94 que4.pop(); 95 } 96 puts(""); 97 puts(""); 98 printf("采用结构体自定义优先级方式二:\n(priority_queue
que)\n"); 99 printf("Queue 5:\n"); 100 while(!que5.empty()){ 101 printf("%3d",que5.top()); 102 que5.pop(); 103 } 104 puts(""); 105 printf("Queue 6:\n"); 106 while(!que6.empty()){ 107 printf("%3d",que6.top()); 108 que6.pop(); 109 } 110 puts(""); 111 return 0; 112 } 113 /*

 

运行结果 : 
采用默认优先关系: 
(priority_queue<int>que;) 
Queue 0: 
 91 83 72 56 47 36 22 14 10  7  3 
 
采用结构体自定义优先级方式一: 
(priority_queue<int,vector<int>,cmp>que;) 
Queue 1: 
  3  7 10 14 22 36 47 56 72 83 91 
Queue 2: 
 91 83 72 56 47 36 22 14 10  7  3 
 
采用头文件"functional"内定义优先级: 
(priority_queue<int,vector<int>,greater<int>/less<int> >que;) 
Queue 3: 
  3  7 10 14 22 36 47 56 72 83 91 
Queue 4: 
 91 83 72 56 47 36 22 14 10  7  3 
 
采用结构体自定义优先级方式二: 
(priority_queue<number>que) 
Queue 5: 
  3  7 10 14 22 36 47 56 72 83 91 
Queue 6: 
 91 83 72 56 47 36 22 14 10  7  3 
*/  
运行结果:
采用默认优先关系:
(priority_queue<int>que;)
Queue 0:
 91 83 72 56 47 36 22 14 10  7  3
采用结构体自定义优先级方式一:
(priority_queue<int,vector<int>,cmp>que;)
Queue 1:
  3  7 10 14 22 36 47 56 72 83 91
Queue 2:
 91 83 72 56 47 36 22 14 10  7  3
采用头文件"functional"内定义优先级:
(priority_queue<int,vector<int>,greater<int>/less<int> >que;)
Queue 3:
  3  7 10 14 22 36 47 56 72 83 91
Queue 4:
 91 83 72 56 47 36 22 14 10  7  3
采用结构体自定义优先级方式二:
(priority_queue<number>que)
Queue 5:
  3  7 10 14 22 36 47 56 72 83 91
Queue 6:
 91 83 72 56 47 36 22 14 10  7  3
好了,如果你仔细看完了上面的代码,那么你就可以基本使用优先队列了,下面给出一些我做题中有过的一些应用,希望能给大家带来一些启
示~
1、先来一个我们最近做的题吧,http://acm.hdu.edu.cn/showproblem.php?pid=1242
题意:某人被关在囚笼里等待朋友解救,问能否解救成功,最少需要多少时间~
具体:可同时有几个朋友,每走一格消耗一分钟的时间 ,地图上还存在着卫兵,卫兵可以解决掉,但是要另外花费一分钟~
分析:从“a”出发,此题可以用回溯法进行深搜,但那样做的话,效率还是不能让人满意,但是广搜的话,由于入队后每次出队时,根据地
图情况的不同,出队元素所记忆的时间并不是层次递增的,因此使用简单广搜的话,同样需要全部搜索才能找到正确答案。有没有一种方法能
让某一步因为遇到士兵而多花时间的结点在队列中向后推迟一层出队呢?答案是肯定的,在这里我们可以用优先队列来实现,总体思想上是,
根据时间进行优先性选择,每次都要出队当前队列元素中记录时间最少的出队,而入队处理时,我们可以按顺序对四个方向上的各种情况按正
常处理入队就行了,出队顺序由优先队列根据预设优先性自动控制。这样,我们就可以从“a”进行基于优先队列的范围搜索了,并且在第一
次抵达有朋友的位置时得到正确结果~具体实现代码:
view plaincopy to clipboardprint?
/*HDU 1242  基于优先队列的范围搜索,16ms   dooder*/  
  
#include
#include
using namespace std; #define M 201 typedef struct p{ int x,y,t; bool operator < (const p &a)const { return t>a.t;//取时间最少优先 } }Point; char map[M][M]; Point start; int n,m; int dir[][2]={
{
1,0},{-1,0},{
0,1},{
0,-1}}; int bfs() { priority_queue
que; Point cur,next; int i; map[start.x][start.y]='#'; que.push(start); while(!que.empty()){ cur=que.top();//由优先队列自动完成出队时间最少的元素 que.pop(); for(i=0;i<4;i++){ next.x=cur.x+dir[i][0]; next.y=cur.y+dir[i][1]; next.t=cur.t+1; if(next.x<0||next.x>=n||next.y<0||next.y>=m) continue; if(map[next.x][next.y]=='#') continue; if(map[next.x][next.y]=='r') return next.t; if(map[next.x][next.y]=='.'){ map[next.x][next.y]='#'; que.push(next); } else if(map[next.x][next.y]=='x'){ map[next.x][next.y]='#'; next.t++; que.push(next); } } } return -1; } int main() { int i,ans; char *p; while(scanf("%d%d",&n,&m)!=-1){ for(i=0;i

 

2、http://acm.hdu.edu.cn/showproblem.php?pid=1053
题意:给出一行字符串,求出其原编码需要的编码长度和哈夫曼编码所需的长度,并求其比值
分析:根据哈夫曼生成树的生成过程可知,其生成树的权值是固定的而且这个值是最小的,而且其值根据生成树的顺序,我们可以找出规律而
不需要真的去生成一棵树然后再求出权值,其模拟过程为取出队列中权值最小的两个元素,将其值加入结果中,然后将这两个元素的权值求和
即得出其父节点的权值,将生成元素作为结点入队~~如此循环,直至取出队列中最后两个元素加入结果,实现代码如下:
view plaincopy to clipboardprint?
/*HDU 1053  采用广搜求哈夫曼生成树的权值 0ms   dooder*/  
#include
#include
#include
#include
#include
using namespace std; #define M 1000050 char str[M]; int list[27]; priority_queue< int,vector
,greater
>que; int main() { int ans,sum; int i,a,b,c; while(scanf("%s",str),strcmp(str,"END")){ memset(list,0,sizeof(list)); for(i=0;str[i];i++){ if(isalpha(str[i])) list[str[i]-'A']++; else list[26]++; } sum=i*8;ans=i;c=0; for(i=0;i<27;i++){ if(list[i]){ que.push(list[i]); c++; } } if(c>1){ans=0;//注意只有一种字符的情况 while(que.size()!=1){ a=que.top(); que.pop(); b=que.top(); que.pop(); ans+=a+b; que.push(a+b); } while(!que.empty())//使用后清空队列 que.pop(); } printf("%d %d %.1f\n",sum,ans,1.0*sum/ans); } return 0; }

 

3、http://acm.pku.edu.cn/JudgeOnline/problem?id=2263
这是第二次练习赛时,我们做过的最后一题,这里采用优先队列进行实现,在《谁说不能这样做题》中已提到这种方法,在这里再次放出代
码,~
题意:给出各城市间道路的限制载重量,求出从一个城市到另外一个城市的贷车能够运载的最大货物重量。
分析:采用优先队列,每次取出当前队列中结点的minheavy最大值出队,对它的连接结点搜索入队,这样,从出发点开始就可以
在到达终点时求出结果,即最大载货物重,实现代码如下:
view plaincopy to clipboardprint?
/*POJ 2263  16ms  dooder*/  
 
#include
#include
#include
using namespace std; #define M 201 typedef struct w{ int city; int mintons; bool operator < (const w &a)const { return mintons < a.mintons; }//优先性定义 }Way; char citys[M][31]; int map[M][M]; bool mark[M][M]; int n,m,from,to,ans,k; priority_queue
que; int min(int a,int b) { return a>b?b:a; } void bfs() { Way cur,next; int i; while(!que.empty()){ cur=que.top(); que.pop(); if(cur.city==to){ if(cur.mintons>ans) ans=cur.mintons; while(!que.empty()) que.pop(); return ; } for(i=0;i
temp){ temp=map[from][i]; index=i; } } cur.city=index; cur.mintons=temp; que.push(cur); bfs(); } int main() { int k1,k2,tons,t=1; char s1[31],s2[31]; while(scanf("%d%d",&n,&m),n||m){ k=0; while(m--){ scanf("%s%s%d",s1,s2,&tons); for(k1=0;strcmp(s1,citys[k1])&&k1

 

当然了,优先队列的用法决不是仅仅提到的这些,各种应用还需要大家去发现,给道题大家可以练习一下hdu 2066\
相信大家已经学到不少了,还有一点可以告诉大家,优先队列是启发式搜索的数据结构基础,希望好好理解,并逐步掌握其用法~
 加:失策啊,竟然忘了说优先队列的效率了,其时间复杂度为O(logn).n为队列中元素的个数,存取都需要消耗时间~

转载地址:http://nyisl.baihongyu.com/

你可能感兴趣的文章
Spring 自动装配及其注解
查看>>
项目部署不到tomcat中的原因和解决方法
查看>>
jUnit Test遇到org.apache.ibatis.binding.BindingException
查看>>
vector排序与查找
查看>>
Py之any函数【转载】
查看>>
将字符串或者数字转化成英文格式输出
查看>>
[9.28模拟] good
查看>>
[NOIP2012] 借教室
查看>>
基于Confluent.Kafka实现的KafkaConsumer消费者类和KafkaProducer消息生产者类型
查看>>
NOI元丹
查看>>
Androidn Notification的使用,解决找不到setLatestEventInfo方法
查看>>
如何改变eclipse控制台编码
查看>>
Python 闭包相关之late binding机制
查看>>
关于复制
查看>>
java AES
查看>>
实验五
查看>>
嵌入式Linux入门经典笔记
查看>>
leetcode29
查看>>
基于服务器的AAA作业(第二次)
查看>>
Objective-c——UI基础开发第十二天(相册展示)
查看>>