PAT

来源:互联网 发布:肝脏再造术的真假知乎 编辑:程序博客网 时间:2024/06/15 09:10

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 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 27.10 07.00 600
Sample Output 2:
The maximum travel distance = 1200.00

给定条件:1.油箱的容量Cmax,目的地的距离D,单位汽油能跑的距离Davg,加油站的个数N2.分别给出N个加油站单位汽油的价钱和距离起点的距离3.汽车开始位置是0,并且没有汽油要求:1.求出汽车开往目的地,最少花费多少钱用来加油2.如果无法到达则输出能到达的最远距离求解:1.如果起点没有加油站,那么哪里也去不了2.如果有加油站,假设加满,那么最远能达到位置X,  试想:1.如果途中有加油站的油更便宜,那么开始不加满,途中再加的话,一定更便宜,但是一定要足够到达这个更便宜的加油站         这时我们到达更便宜的加油站,继续假设加满油,那么能跑到最远距离是Y,如果途中....分析到这里已经出现重复的子问题。       2.如果途中没有更便宜的,那么开始就加满油肯定是最好的。但是途中我们也得加油,不然跑完没油了,需要找到的是其中相对便宜的加油站         这时我们到达这个相对便宜的加油站,假设加满油,那么最远能跑的距离是Z,如果途中....这里在此出现重复子问题。       3.分析完毕,开始写程序。注解:1.leftDis表示加满油后,跑到相对最便宜的加油站,剩余油量还能跑多少距离2.nowDis表示当前位置("起点")

#include <cstdio>#include <algorithm>using namespace std;struct Station {int dis;double price;} sta[510];bool cmp1(Station sta1, Station sta2) {return sta1.dis < sta2.dis;}int cmax, d, dav, n;int main() {scanf("%d%d%d%d", &cmax, &d, &dav, &n);for(int i = 0; i < n; i++) {scanf("%lf%d", &sta[i].price, &sta[i].dis);}sta[n].price = 0; sta[n].dis = d;sort(sta, sta+n+1, cmp1);if(sta[0].dis != 0) {printf("The maximum travel distance = 0.00\n");return 0;} else {double maxDis, nowDis = 0.0, ans = 0.0, nowPrice = sta[0].price, leftDis = 0.0;while(nowDis < d) {maxDis = nowDis + cmax * dav;int flag = 1;int minId; double minPirce = 99999999;for(int i = 1; i <= n; i++) {if(sta[i].dis <= nowDis || sta[i].dis > maxDis) continue;if(sta[i].price < nowPrice) {ans += (sta[i].dis - nowDis - leftDis) / dav * nowPrice;leftDis = 0.0;nowPrice = sta[i].price;nowDis = sta[i].dis;flag = 0;break;}if(sta[i].price < minPirce) {minPirce = sta[i].price;minId = i;}}if(flag && minPirce != 99999999) {ans += ((cmax-leftDis/dav) * nowPrice);leftDis = cmax * dav - (sta[minId].dis - nowDis);nowPrice = sta[minId].price;nowDis = sta[minId].dis;} if(flag && minPirce == 99999999) {printf("The maximum travel distance = %.2f\n", maxDis);return 0;}}printf("%.2f\n", ans);}return 0;}

原创粉丝点击