题目描述:
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: 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.
输入输出示例:
输入
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
输出:749.17
输入
50 1300 12 2
7.10 0
7.00 600
输出:The maximum travel distance = 1200.00
具体思路:
(1)首先结果都要求保留两位小数
(2)不能到达终点的情况
(2.1)初始加油站与起点的距离不为0
(2.2)两个加油站之间的距离大于汽车满油时能跑的最大距离
(3)用number记录当前所在加油站的编号,如果当前所在加油站可到范围内有价格比当前加油站价格便宜的加油站k1,如果当前邮箱内的油足够行驶到k1,那么直接行驶过去,如果不能,那么在当前加油站加油加到能够行驶到k1即可
(4)如果当前所在加油站可到范围内没有比当前加油站价格便宜的加油站,如果终点不在可到范围内,那么就从当前加油站可到范围中选一个价格相对便宜的加油站k开过去,注意此时要在油箱内加满油(因为当前的是最便宜的)。如果终点在可到范围内,那么直接加油加到能够开到终点(如果剩余油量够到终点可以直接开过去)。
(5)如果在当前加油站可到范围内既没有比当前价格便宜的加油站也没有相对便宜的加油站(即可到范围内没有加油站),如果终点在可到范围内,那么直接加油加到可以开到终点,如果终点不在范围内,那么输出最大行驶距离即可
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
double cmax, D, Davg;
int N;
struct node
{
double distance;//距离起点的距离
double price;//加油站价格
};
bool cmp(node a, node b)
{
return a.distance<b.distance;
}
int findmin(node *a, int N, int number)
{
double jojo=10000000;
int k=-1;
for(int i=number+1;i<N;i++)
{
if(a[i].distance-a[number].distance<=cmax*Davg)
{
if(a[i].price<jojo)
{
jojo=a[i].price;
k=i;
}
}
else
{
break;
}
}
return k;
}
int findcurrentmin(node *a, int N, double price, int number)
{
int k=-1;
for(int i=number+1;i<N;i++)
{
if(a[i].distance-a[number].distance<=cmax*Davg)
{
if(a[i].price<price)
{
k=i;
break;
}
}
}
return k;
}
int main()
{
double distance=0, price=0;
double now=0;
int number=0;
cin>>cmax>>D>>Davg>>N;
double len=cmax*Davg;
node s[N];
for(int i=0;i<N;i++)
{
cin>>s[i].price>>s[i].distance;
}
sort(s, s+N, cmp);
if(s[0].distance!=0)
{
cout<<"The maximum travel distance = 0.00\n";
return 0;
}
while(distance<D)
{
int k=findmin(s, N, number);//k是当前可到范围内相对便宜的加油站
int k1=findcurrentmin(s, N, s[number].price, number);//k1是当前所到范围内第一家比当前价格便宜的加油站
if(k1==-1&&s[number].distance+len<D)//没有比当前价格便宜的加油站且终点不在可到范围内
{
if(k!=-1)//在当前加油站加满油,找到相对便宜的加油站k开过去
{
price+=(cmax-now)*s[number].price;
distance+=s[k].distance-s[number].distance;
now=cmax-(s[k].distance-s[number].distance)/Davg;
number=k;
}
else//输出最大可到距离
{
printf("The maximum travel distance = %.2lf\n",len+s[number].distance);
return 0;
}
}
else if(k1==-1&&s[number].distance+len>=D)//没有比当前价格更便宜的加油站且终点在可到范围内,直接加油到终点
{
if((D-distance)/Davg-now>=0)
{
price+=((D-distance)/Davg-now)*s[number].price;
break;
}
else
{
break;
}
}
else//如果有比当前价格便宜的加油站,加油到能够开到k1
{
if((s[k1].distance-s[number].distance)/Davg-now>0)
{
price+=((s[k1].distance-s[number].distance)/Davg-now)*s[number].price;
distance+=s[k1].distance-s[number].distance;
number=k1;
now=0;
}
else
{
distance+=s[k1].distance-s[number].distance;
number=k1;
now-=(s[k1].distance-s[number].distance)/Davg;;
}
}
}
printf("%.2lf\n",price);
}