POJ_2431 (Expedition)

来源:互联网 发布:linux更新软件源 编辑:程序博客网 时间:2024/06/13 04:26
Expedition
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 18008
Accepted: 5259

Description

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.

Input

* Line 1: A single integer, N

* 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.

* Line N+2: Two space-separated integers, L and P

Output

* 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.

Sample Input

44 45 211 515 1025 10

Sample Output

2

题意:有奶牛乘坐拖拉机去丛林目的地,但是不幸的是拖拉机撞到了石头,导致出现漏油,每公里漏一单位的油,所以奶牛要想到达目的地必须要前往加油站加油,路途中共有N个加油站,每个加油站都有一定数量的油,给出加油站距离目的地的距离和加油站存储油量,问奶牛需要往加油站加几次油才能到达目的地。

解题分析:奶牛最初有X的油,可以经过一些加油站,将这些加油站的存储油量分别放入优先队列(最大值位于队列顶)中,假设最初油不够到下一站时,取出优先队列队顶元素,当做奶牛去过这个加油站加过油,当油量足够到下一站时,将下一站的剩余油量进队列,如果队列取空了还不能到达目的地,则奶牛不可能到达目的地,输出 "-1" 。
AC  code :

#include<cstdio>#include<algorithm>#include<iostream>#include<queue>using namespace std;struct st{int site;int available;};int cmp(st a,st b){//按照距离进行排序 return a.site < b.site; }int main(void){int flag = 0;int i;int ans = 0;int sta_number;int goal;long long fuel;priority_queue<int>pque;//定义优先队列 struct st sta[100005];scanf("%d",&sta_number);for(i=0;i<sta_number;i++)scanf("%d%d",&sta[i].site,&sta[i].available);scanf("%d%lld",&goal,&fuel);for(i=0;i<sta_number;i++)//将其转化为到起点的距离 sta[i].site = goal - sta[i].site;//按到起点距离进行排序 sort(sta,sta+sta_number,cmp);for(i=0;i<sta_number;i++){if(fuel >= sta[i].site){//如果能跑过第 i 站,则将其进队列pque.push(sta[i].available); }//如果不能跑过第 i 站else{//取队顶元素,直到能到下一站或者队列为空 while(!pque.empty() && fuel < sta[i].site){fuel = fuel + pque.top();pque.pop();//取出,相当于到过加油站 ans++;}//如果队列空而仍不能到下一站,奶牛就到不了目的地 if(fuel < sta[i].site){flag = 0;break;}//else pque.push(sta[i].available); }//如果中途就有了足够到目的地的油,则当前ans即为答案,循环结束 if(fuel >= goal ){flag = 1;break;}}//循环结束,可能队列中元素没取尽,也没到达终点 while(!pque.empty() && fuel < goal){fuel = fuel + pque.top() ;ans++;pque.pop();}if(fuel < goal ) flag = 0;else flag = 1;if(flag == 0) printf("%d\n",-1);else printf("%d\n",ans);return 0;} 



原创粉丝点击