您的位置:首页 > 其它

poj 2431 Expedition(优先队列+贪心)

2017-12-25 22:20 411 查看
原题地址:http://poj.org/problem?id=2431

题意:给定n个加油站,每个加油站有2个信息,一个是距离终点站的长度,一个是可加的油量,现在给出路程总长度和和初始油量,让你判断是否能将车开到终点站,若能输出最少加油次数,若不能,输出-1

Expedition

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 19168

Accepted: 5523

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

4

4 4

5 2

11 5

15 10

25 10

Sample Output

2

Hint

INPUT DETAILS:

The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively.

OUTPUT DETAILS:

Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.

分析:1.首先数据是两个有关系的,所以考虑用结构体来存储

2.然后是为了解题方便,考虑将最后的终点站也考虑成加油站,不过是可加油的数量为0的加油站

3.现在考虑如何加油是最优的,显然我们每经过一个加油站就去考虑是否要去加这个加油站的油是不合理的,因为这样子太烦琐了,所以可以考虑换个角度。我们可以这么思考,用优先队列来存储数据,每当经过一个加油站,就可以将这个加油站存入优先队列,一旦发现油不够了,这时就可以使用任何一个之前经过的加油站的油,那么最优的方案显然是将油量最多的加油站的油加到车子中。

4.退出的条件是什么?很容易想到,当你需要油的时候却发现优先队列已经空了,这很明显是不满足题意的,(因为终点站是最后一个加油站,只有当你开到了最后的加油站,才满足题意)

下面是ac代码

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define inf 0x3f3f3f3f
#define eps 1e-8
typedef long long ll;
using namespace std;
struct node
{
int x;
int y;
bool operator <(const node a) const
{
return y<a.y;
}
} q[10005];
bool cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
int n,distance,sum;
priority_queue<node>que;
while(~scanf("%d",&n))
{
int ans=0,flag=1;//flag用于标记是否满足题意
for(int i=0; i<n; i++)
{
scanf("%d%d",&q[i].x,&q[i].y);
}
scanf("%d%d",&distance,&sum);
for(int i=0; i<n; i++)
{
q[i].x=distance-q[i].x;//因为题目数据是离终点站的距离,所以为了方便计算,将其转化为离起点站的距离
}

q
.x=distance;
q[n++].y=0;
sort(q,q+n,cmp);
for(int i=0; i<n; i++)
{
if(q[i].x<=sum)
{
que.push(q[i]);
}
while(q[i].x>sum)
{
if(que.empty())
{
flag=0;
break;
}
sum+=que.top().y;
que.pop();
ans++;
i--;//如果没有i--,就会导致没有判断q[i]的入列情况,会wa
}
}
if(flag==0) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj