UVALive 3135--Argus+自己定义优先队列的优先规则

题目链接:点击进入
仅仅是题意比較难懂,读懂题后全然能够用优先队列水过去.这次学会自己定义优先队列的优先规则,事实上就是在结构体中重载一下<运算符.

代码例如以下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

typedef struct node
{
     int id,num;
     int t;
     ///通过重载<运算符自己定义优先队列的优先级
     friend bool operator< (node n1,node n2)
     {
           if(n1.num==n2.num)
               return n1.id>n2.id;
           return n1.num>n2.num;
     }
}P;
P p1,p2;

priority_queue<P>q;

int main()
{
      char str[20];
      int a,b;
      //freopen("in.txt","r",stdin);
      while(scanf("%s",str))
      {
            if(strcmp(str,"#")==0) break;
            scanf("%d%d",&p1.id,&p1.num);
            p1.t=p1.num;
            q.push(p1);
      }
      int k;
      scanf("%d",&k);
      while(k--)
      {
             p2=q.top();  q.pop();
             printf("%d
",p2.id);
             p2.num+=p2.t;
             q.push(p2);
      }
   return 0;
}
原文地址:https://www.cnblogs.com/yxwkf/p/5395735.html