HDU 1896 Stones --优先队列+搜索

一直向前搜。。做法有点像模拟。但是要用到出队入队,有点像搜索。

代码:

#include <iostream>

#include <cstdio>

#include <cstring>

#include <cmath>

#include <algorithm>

#include <queue>

using namespace std;

#define N 100003



struct node

{

    int p,d;

    bool operator <(const node &a)const

    {

        if(p == a.p)

            return d>a.d;

        return p>a.p;

    }

}stone[N];



int maxdis;

priority_queue<node> que;



void GO()

{

    node now,next;

    int OE = 1;

    while(!que.empty())

    {

        now = que.top();

        que.pop();

        if(OE)

        {

            next.p = now.p + now.d;

            next.d = now.d;

            que.push(next);

            maxdis = max(next.p,maxdis);

            OE = 0;

        }

        else

            OE = 1;

    }

}



int main()

{

    int t,i,p,d,n;

    scanf("%d",&t);

    while(t--)

    {

        maxdis = 0;

        scanf("%d",&n);

        for(i=0;i<n;i++)

        {

            scanf("%d%d",&stone[i].p,&stone[i].d);

            que.push(stone[i]);

        }

        GO();

        printf("%d\n",maxdis);

    }

    return 0;

}
View Code

 

你可能感兴趣的:(优先队列)