您的位置:首页 > 其它

1033. To Fill or Not to Fill (25)-PAT甲级真题

2017-11-30 11:09 435 查看
1033 To Fill or Not to Fill (25)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,…N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print “The maximum travel distance = X” where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:

50 1300 12 8

6.00 1250

7.00 600

7.00 150

7.10 0

7.20 200

7.50 400

7.30 1000

6.85 300

Sample Output 1:

749.17

Sample Input 2:

50 1300 12 2

7.10 0

7.00 600

Sample Output 2:

The maximum travel distance = 1200.00

  分析:这道题意思是,一辆车,开始没有油,一条线从起点到终点,途中有加油站,反正都在一条线上,油钱有高有低,问怎么最便宜到目的地。

  这样其实很清楚了,先判断,起点有没有加油站,没有就gg,直接跑不了。然后开始贪心的算法,每一步最优解,每一步去找接下去的加油站,如果油价便宜,我就加到足够开到那一个加油站的油,去那边加油。如果找不到油价更低了的,那就得把油全部加满了,因为当前的加油站是最便宜的,然后找尽可能小的又能走到的加油站去加油。当然油装满了都跑不到下一个点,那么肯定就gg了。

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int inf = 99999999;
struct station
{
double Pi , Di;
};
bool cmp (station a , station b)
{
return a.Di < b.Di;
}
int main ()
{
double cmax , d , davg;
int n;
scanf_s ("%lf%lf%lf%d" , &cmax , &d , &davg , &n);
vector<station> sta (n + 1);
//加上一个终点站
sta[0].Pi = 0.0;
sta[0].Di = d;
for (int i = 1; i <= n; i++)
{
scanf_s ("%lf%lf" , &sta[i].Pi , &sta[i].Di);
}
sort (sta.begin () , sta.end () , cmp);
double nowdis = 0.0 , maxdis = 0.0 , nowprice = 0.0 , totalPrice = 0.0 , leftdis = 0.0;
if (sta[0].Di != 0)
{
printf ("The maximum travel distance = 0.00");
return 0;
}
else
{
nowprice = sta[0].Pi;
}
//排序判断完之后,就是起点是0开始了,下面就一步步开始判断下面的加油站
while (nowdis < d)
{
//保存当前加油站加满后能跑到的最大距离
maxdis = nowdis + cmax * davg;
double minPriceDis = 0 , minPrice = inf;
int flag = 0;
for (int i = 1; i <= n && sta[i].Di <= maxdis; i++)
{
if (sta[i].Di <= nowdis) continue;
//有更便宜的,加到跑到i加油站的油,正好到加油站leftdis=0
if (sta[i].Pi < nowprice)
{
totalPrice += (sta[i].Di - nowdis - leftdis) * nowprice / davg;
leftdis = 0.0;
nowprice = sta[i].Pi;
nowdis = sta[i].Di;
flag = 1;
break;
}
//得出较便宜的加油站
if (sta[i].Pi < minPrice)
{
minPrice = sta[i].Pi;
minPriceDis = sta[i].Di;
}
}
//加满油前往相对便宜的那个加油站
if (flag == 0 && minPrice != inf)
{
totalPrice += (nowprice * (cmax - leftdis / davg));
leftdis = cmax * davg - (minPriceDis - nowdis);
nowprice = minPrice;
nowdis = minPriceDis;

}
if (flag == 0 && minPrice == inf)
{
nowdis += cmax * davg;
printf ("The maximum travel distance = %.2f" , nowdis);
return 0;
}
}
printf ("%.2f" , totalPrice);
return 0;
}


  最后我想了很久的是,最后终点怎么处理,如果光有N个加油站,那么判断到达了最后一个加油站就前进不了了,而且完全无法判断,万一我在一个加油站加满能够到终点,那为啥还要加满去下一个相对便宜的加油站呢。所以直接加上一个终点的节点,价格为0,要是能走到,直接前往这个点。万事大吉!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: