本文共 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.
之后按距离升序排序 我们可以先求出从一个加油站出发所到达的最远距离,然后从能到达的加油站里面挑选下一个目的地。这里分两种情况。#includeusing 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/