STL之priority queue

优先队列(priorityqueue),顾名思义就是建立一个队列,在元素对位增加,从队列头部删除。在有限队列中,元素被赋予优先级,具有最高优先级的元素先删除。优先队列具有最高优先级先出的行为特征。

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

struct cmp1{
	bool operator ()(int &a,int &b){
		return a > b;
	}
};
struct cmp2{
	bool operator()(int &a,int &b){
		return a < b;
}
};
struct number1{
	int x;
	bool operator < (const number1 &a)const{
	   return x > a.x;
	}
};
struct number2{
	int x;
	bool operator < (const number2 &a)const{
	  return x < a.x;
	}
};
int a[] = {14,10,12,5,2,3,55,2,51,20,0};
number1 num1[] = {14,10,12,5,2,3,55,2,51,20,0};
number2 num2[] = {14,10,12,5,2,3,55,2,51,20,0};
int main()
{
	priority_queue<int>que;
	priority_queue<int,vector<int>,cmp1>que1;
	priority_queue<int,vector<int>,cmp2>que2;
	priority_queue<int,vector<int>,greater<int> >que3;
	priority_queue<int,vector<int>,less<int> >que4;
	priority_queue<number1>que5;
	priority_queue<number2>que6;
	int i;
	for(i = 0;a[i];i++)
	{
		que.push(a[i]);
		que1.push(a[i]);
		que2.push(a[i]);
		que3.push(a[i]);
		que4.push(a[i]);
	}
	for(i = 0;num1[i].x;i++)
	que5.push(num1[i]);
	for(i = 0;num2[i].x;i++)
	que6.push(num2[i]);
	
	printf("采用默认优先关系:\n(priority_queue<int>que)\n");
	printf("Queue 0:\n");
	while(!que.empty())
	{
		printf("%3d",que.top());
		que.pop();
	}
	puts("");
	puts("");
	printf("采用结构体定义的优先级方式:\n(peiority_queue<int,vector<int>,cmp>que)\n");
	printf("Queue 1:\n");
	while(!que1.empty())
	{
		printf("%3d",que1.top());
		que1.pop(); 
	}
	puts("");
	printf("Queue 2:\n");
	while(!que2.empty())
	{
		printf("%3d",que2.top());
		que2.pop();
	}
	puts("");
	puts("");
	printf("采用头文件\"functional\"内定义优先级:\n(priority_queue<int>,greater<int>/less<int> >que)\n");
	printf("Queue 3:\n");
	while(!que3.empty())
	{
		printf("%3d",que3.top());
		que3.pop();
	}
	puts("");
	printf("Queue 4:\n");
	while(!que4.empty())
	{
		printf("%3d",que4.top());
		que4.pop();
	}
	printf("采用结构体自定义优先级方式二:\n(priority_queue<number1>que)\n");
	while(!que5.empty())
	{
		printf("%3d",que5.top());
		que5.pop();
	}
	puts("");
	printf("Queue 6:\n");
	while(!que6.empty())
	{
		printf("%3d",que6.top());
		que6.pop();
	}
	cout << endl;
	return 0;
 } 

看代码理解(学校大神附送,非常详细),我就不展开讲了。

例题:

ZOJ-1242

http://acm.hdu.edu.cn/showproblem.php?pid=1242

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34942    Accepted Submission(s): 12163


Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
 

Input
First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend. 

Process to the end of the file.
 

Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life." 
 

Sample Input
 
 
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
 

Sample Output
 
 
13
 

Author
CHEN, Xue
 

Source
 

Recommend
Eddy

AC:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;

char a[100][100];
int d[100][100];
int m,n;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int x1,y1,x2,y2;
struct acc{
    int x;
    int y;
    int temp;
    bool friend < (acc s1,acc s2){
        return s2.temp < s1.temp;
    }
};
bool check(int x,int y)
{
    if(x < 0 || y < 0 || x >= m || y >= n || a[x][y] != '.' || d[x][y] == 0)
        return false;
    return true;
}
int bfs()
{
    acc k,l;
    l.temp = 0;
    l.x = x1;
    l.y = y1;
    priority_queue<acc>que1;
    que1.push(l);
    d[x1][y1] = 0;
    while(!que1.empty())
    {
        l = que1.top();
        que1.pop();
        if(l.x == x2 && l.y == y2)
            return l.temp;
        for(int i = 0;i < 4;i++)
        {
            k = l;
            k.x = k.x + dx[i];
            k.y = k.y + dy[i];
            if(check(k.x,k.y))
            {
                que1.push(k);
                //d[k.x][k.y] = 0;
                k.temp++;
            }
        }
    }
    return 0;
}
int main()
{
    cin >> m >> n;
    for(int i = 0;i < m;i++)
    {
        for(int j = 0;j < n;j++)
        {
            cin >> a[i][j];
            if(a[i][j] == 'x')
            {
                x1 = i;
                y1 = j;
            }
            else if(a[i][j] == 'a')
            {
                x2 = i;
                y2 = j;
            }
        }
    }
    memset(d,1,sizeof(d));
    int  t = bfs();
    if(t)
        cout << "NO WAY" << endl;
    else
        cout << t << endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值