Expedition -贪心+优先队列

【题目】

A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.

To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop).

The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).

Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.

【输入】

* Line 1: A single integer, N <br> <br>* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop. <br> <br>* Line N+2: Two space-separated integers, L and P

【输出】

* Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.

【样例】

输入:

4
4 4
5 2
11 5
15 10
25 10

输出:

2

题目大意:现有l长的一段路程,行走1个单位消耗一个单位的油量,路途中有n个加油站,在加油站中可以获得一定的油(油箱无限大)初始有一定的油量p,走完l路程最少加油数目,走不完输出-1;

思路:

  1. 将各个加油站以距终点距离优先的升序排序;
  2. 先把各个站点的油放进优先队列中,有用的时候拿出来加上;
  3. 中途优先队列中的油用完了还不能到达下一个站点,则走不完;

代码:

#include<cstdio>
#include<queue>
#include<algorithm>
#define N 10010
using namespace std;

struct edge
{
    int pos,fuel;
}station[N];

int cmp(edge a,edge b)
{
    return a.pos<b.pos;//距离优先

int p,l,n;
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d%d",&station[i].pos,&station[i].fuel);
    scanf("%d%d",&l,&p);

    for(int i=0;i<n;i++)
        station[i].pos=l-station[i].pos; //将距离换为站点到终点的距离
    station[n].pos=l;//终点算作最后一个站点
    station[n].fuel=0;

    sort(station,station+n+1,cmp);

    priority_queue<int>q;
    int flag=1;
    int ans=0,pos=0,res_fuel=p; //pos记录当前行走路程起点位置,res_fuel为油箱里的油量

    for(int i=0;i<n+1;i++)
    {
        int dis=station[i].pos-pos;
        while(res_fuel<dis)
        {
            if(q.empty())
            {
                flag=0;
                break; //队列中的油用完了
            }
            res_fuel=res_fuel+q.top();  //用油
            q.pop();
            ans++;
        }

        if(flag==0)
            break;
        res_fuel=res_fuel-dis;
        pos=station[i].pos;
        q.push(station[i].fuel); //存油
    }
    if(flag==0)
        printf("%d\n",-1);
    else
        printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wentong_Xu/article/details/81257462
今日推荐