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 300Sample Output 1:
749.17Sample Input 2:
50 1300 12 2 7.10 0 7.00 600Sample Output 2:
The maximum travel distance = 1200.00
AC代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
struct Sta {
double price, distance;
}sta[505];
bool cmp(Sta a, Sta b) {
return a.distance < b.distance;
}
int main() {
double tank, dis, davg;
int n;
scanf("%lf%lf%lf%d", &tank, &dis, &davg, &n);
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &sta[i].price, &sta[i].distance);
}
//如果不采取其他措施,则这一步必不可少,否则会在第4检查点出错
//因为一直在比较maxdis之间的station的油价,当price为0时显然最低
//所以,设置destination的price为0
//利用这种方法则显得十分便利
sta[n].price = 0;
sta[n].distance = dis;
bool finish = true;
sort(sta, sta + n, cmp);
if (sta[0].distance != 0) {
printf("The maximum travel distance = 0.00");
return 0;
}
double maxdis = davg*tank, nowtank = 0.0, sum = 0.0, money = 0.0;
int info , now = 0;
const double MIN = 1<<31-1;
while (sum < dis) {
double minprice = MIN;
bool lower = false;
info = -1;
int i = now+1;
double nowdis = sta[now].distance;
while (i<=n&&sta[i].distance-nowdis <= maxdis) {
if (minprice > sta[i].price) {
minprice = sta[i].price;
info = i;
if (minprice < sta[now].price) {
lower = true;
break;
}
}
i++;
}
if (info==-1) {
if (dis - sta[now].distance <= maxdis) {
double need = (dis-sta[now].distance) / davg;//油
if (nowtank < need) {
money += (need - nowtank)*sta[now].price;
}
}
else {
finish = false;
sum += maxdis;
}
break;
}
if (lower) {
double need = (sta[info].distance-nowdis) / davg;//油
if (nowtank < need) {
money += (need - nowtank)*sta[now].price;
nowtank = 0;//到达下一站之后所剩的油量
}
else {
nowtank -= need;
}
sum = sta[info].distance;
now = info;
}
else {
money += (tank - nowtank)*sta[now].price;
nowtank = tank - (sta[info].distance - nowdis) / davg;
now = info;
sum = sta[info].distance;
}
}
if (finish) {
printf("%.2f",money);
}
else {
printf("The maximum travel distance = %.2f", sum);
}
return 0;
}
有部分借鉴于书本,但不全是,所以代码略显臃肿。