ZOJ 2724 Windows Message Queue (二叉堆,优先队列)

思路:用优先队列 priority_queue,简单

两种方式改变队列 的优先级 (默认的是从大到小)

 

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

struct node
{
    char name[100];
    int para;
    int pri;
    int t;
};
/*
struct cmp
{
    bool operator ()(node a,node b)
    {
		if(a.pri==b.pri)
			return a.t>b.t;
		return a.pri>b.pri;
    }
};
*/
bool operator< (node a,node b)     //   代码2
{
	if(a.pri==b.pri)
		return a.t>b.t;
	return a.pri>b.pri;
}

int main()
{
    node temp;
//  priority_queue<node,vector<node>,cmp> q;

	priority_queue<node>q;   //   代码2

    char s[5];
    int m;
    m=0;
    while(scanf("%s",s)!=EOF)
    {
        if(s[0]=='G')
        {
            if(!q.empty())
            {
                temp=q.top();//top()  返回优先队列中有最高优先级的元素 
                q.pop();//pop()  删除第一个元素 
                printf("%s %d\n",temp.name,temp.para);
            }
            else
                printf("EMPTY QUEUE!\n");
        }
        else if(s[0]=='P')
        {
            m++;
            scanf("%s%d%d",temp.name,&temp.para,&temp.pri);
            temp.t=m;
            q.push(temp);//push()  加入一个元素 
        }
    }
    return 0;
}


 

posted @ 2014-08-03 12:00  gongpixin  阅读(185)  评论(0编辑  收藏  举报