您的位置:首页 > 其它

优先队列的应用

2016-07-07 20:27 225 查看
hdu1896 点击打开链接

#include"iostream"
#include"cstdio"
#include"cstdlib"
#include"cstring"
#include"queue"

using namespace std;

typedef struct node
{
int pos;
int weight;
}point;

struct cmp
{
bool operator()(const point a,const point b)
{
if(a.pos!=b.pos) return a.pos>b.pos; //>后置
else return a.weight>b.weight; //>后置
}
};

int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
priority_queue<node,vector<node>,cmp> q;
point stone;
while(n--)
{
cin>>stone.pos>>stone.weight;
q.push(stone);
}
stone=q.top();
bool is=1;
while(!q.empty())
{
if(is==1)
{
stone=q.top();
stone.pos+=stone.weight;
q.push(stone);
q.pop();
}
else
{
stone=q.top();
q.pop();
}
is=!is;
}
cout<<stone.pos<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: