Priority Queue (Priority_queue) for Cmp,poj (2051)

Source: Internet
Author: User

The sort () function of the CMP is a function, the Priority_queue CMP is a class, the specific wording is:

 struct   Node { int   I,j;} node[ 200  ]; struct   cmp { bool  operator   () (Node A,node b) { if  (A.i==B.I)  return  a.j<b.j; ///        j Ascending     return  a.i<b.i; /// i Ascending   <Node,vector<Node>,cmp> re; 

#include <iostream>#include<queue>using namespaceStd;typedefstruct {    intId///User ID    intTime///The time of the next occurrence    intPeriod///time interval} Register;///to define a comparison function for a priority queuestructCMP {BOOL operator() (Register A,register b) {if(a.time==b.time)returna.id>b.id;///Sorted by ID in ascending order when time is equal        returna.time>b.time;///in ascending order of time, to be the smallest heap    }};intMain () {///define the priority queue, first to the smallest arrival time, and in ascending order by ID when equalpriority_queue<register,vector<register>,cmp >re; stringcmd///Command    intId///Define ID    intPeriod///defining the time interval    intCount///define the number of output bars     while(true) {cin>>cmd; if(Cmd.compare ("#")==0) Break; CIN>>id>>period;        Register R; R.id=ID; R.period=period; R.time=period;    Re.push (R); } CIN>>count;  while(count--) {Register reg=Re.top (); cout<<reg.id<<Endl; Reg.time+=Reg.period;        Re.pop ();    Re.push (REG); }    return 0;}
View Code

Priority Queue (Priority_queue) for Cmp,poj (2051)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.