博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT (Advanced Level) Practice 1033 To Fill or Not to Fill(贪心)
阅读量:3904 次
发布时间:2019-05-23

本文共 2750 字,大约阅读时间需要 9 分钟。

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: C
​max
​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D
​avg
​​ (≤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: P
​i
​​ , the unit gas price, and D
​i
​​ (≤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

题意

我们需要从杭州开车前往另一个城市,给出两个城市之间的距离,油箱的容量,每一单位的油能行驶的距离,以及经过的加油站数,给出每个加油站的油的价格以及距离杭州的距离,一开始油箱是空的,若能到达目的地,则输出最少的费用,反之,则输出最大行驶距离。

思路

首先,如果没有在距离等于0的加油站,则直接输出最大行驶距离为0.

之后按距离升序排序
我们可以先求出从一个加油站出发所到达的最远距离,然后从能到达的加油站里面挑选下一个目的地。这里分两种情况。

  1. 能到达的加油站中有比所在加油站价格便宜的
    这种情况下,我们优先选择距离最近的比所在加油站价格便宜的加油站
    为什么不选择最小价格的加油站呢,下面一张图解释
    在这里插入图片描述
    这种情况下,我们把油加到正好到达目标加油站即可。
  2. 能到达的加油站都比起点加油站贵
    这里我们选择将油都加满,然后选择能到达加油站中价格最便宜的作为目的地。
    我们可以将目的地也当成一个加油站,油价为0,当我们到达这个加油站的时候就跳出循环,输出最少花费钱数。

代码如下

#include 
using namespace std;const int maxn=505;const int INF=0x3f3f3f3f;int n;double d,c,davg;struct node{
double dis; double p;};node a[maxn];int compare (node x,node y){
if(x.dis!=y.dis) return x.dis
a[now_loc].dis+per_maxdis) {
printf("The maximum travel distance = %.2lf\n",a[now_loc].dis+per_maxdis); return 0; } for (int i=now_loc+1;i<=n;i++) {
if(a[i].dis>a[now_loc].dis+per_maxdis) break; if(a[i].p<=a[now_loc].p) {
min_pri=a[i].p; min_loc=i; break; } } //对应着情况2 if(min_loc==-1) {
min_loc=now_loc+1; for (int i=now_loc+2;i<=n;i++) {
if(a[i].dis>a[now_loc].dis+per_maxdis) break; if(a[i].p

转载地址:http://zcaen.baihongyu.com/

你可能感兴趣的文章
小米笔记本安装Win 10历程
查看>>
【转】SLAM 论文阅读和分类整理
查看>>
【转】Ubuntu 16.04 重置密码(忘记密码)
查看>>
【转】信息奥赛一本通1185:单词排序(OJ题目描述有问题)
查看>>
【转】在EXCEL表格中如何用厘米毫米来设置行高列宽?
查看>>
开源spider
查看>>
HttpUnit: 一种在 WebSphere Studio 中测试 Web 应用程序的改进方式
查看>>
Python Self
查看>>
webclient
查看>>
从百度MP3搜索结果中提取歌曲列表
查看>>
python模块之HTMLParser
查看>>
模拟IE(FireFox)的Spider技术介绍
查看>>
去除文本中的空行的bash命令
查看>>
Sift Applcation
查看>>
我网易的blog
查看>>
linux下启动mysql
查看>>
进入mysql命令行管理模式
查看>>
Writing MySQL Scripts with Python DB-API
查看>>
What To Do If mysql Cannot Be Found
查看>>
浅谈ASP.NET的Postback
查看>>