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

来源:互联网 发布:私人做网络小贷平台 编辑:程序博客网 时间:2024/06/06 00:51

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
#include<iostream>#include <cstdio>#include <algorithm>#include <vector>using namespace std;const int inf = 99999999;struct station {double price, dis;};bool cmp1(station a, station b) {return a.dis < b.dis;}int main() {double cmax, d, davg;int n;scanf("%lf%lf%lf%d", &cmax, &d, &davg, &n);vector<station> sta(n + 1);sta[0].price = 0.0;sta[0].dis = d;for (int i = 1; i <= n; i++) {scanf("%lf%lf", &sta[i].price, &sta[i].dis);}sort(sta.begin(), sta.end(), cmp1);double nowdis = 0.0, maxdis = 0.0, nowprice = 0.0, totalprice = 0.0, leftdis = 0.0;if (sta[0].dis != 0) {printf("The maximum travel distance = 0.00");return 0;}else {nowprice = sta[0].price;}while (nowdis < d) {maxdis = nowdis + cmax*davg;double minpricedis = 0, minprice = inf;int flag = 0;for (int i = 1; i <= n&&sta[i].dis <= maxdis; i++) {if (sta[i].dis <= nowdis) continue;if (sta[i].price < nowprice) {totalprice += (sta[i].dis - nowdis - leftdis)*nowprice / davg;leftdis = 0.0;nowprice = sta[i].price;nowdis = sta[i].dis;flag = 1;break;}if (sta[i].price < minprice) {minprice = sta[i].price;minpricedis = sta[i].dis;}}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;}

原创粉丝点击