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 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
代码长度限制16 KB
时间限制200 ms
内存限制64 MB
//参考Reza.思路讲解http://t.csdn.cn/nLn8e
//参考柳婼代码http://t.csdn.cn/ul8RQ
#include<iostream>
#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;
cin>>cmax>>d>>davg>>n;
vector<station> sta(n+1);
sta[0]={0.0,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){//如果初始没有距离为0的加油站,既无法开出去
cout<<"The maximum travel distance = 0.00";
return 0;
}else{
nowprice = sta[0].price;
}
int now=0;
while(nowdis<d){//不断循环直到到达目的地
maxdis = nowdis+cmax*davg;
double mp=inf;
//vector<double> minPrice;
//fill(minPrice,minPrice+501,inf);
int ms;
//vector<int> minsta;
//int flag=0;
int i;
for(i=now+1;i<=n&&sta[i].dis<=maxdis;i++){//寻找当前你可达的最远范围内比你当前站点便宜的站,每次只加刚好能到其距离所需油费,
if(sta[i].price>nowprice&&sta[i+1].dis>maxdis&&i<n){//若没有找一个相对便宜的
for(int j=now+1;j<=i;j++)
if(sta[j].price<mp){
mp=sta[j].price;
ms=j;
}
}
else if(sta[i].price<=nowprice){//找到便宜站点就更新
totalPrice=totalPrice+nowprice*((sta[i].dis-nowdis-leftdis)/davg);
// cout<<" -"<<totalPrice<<"- \n";
nowdis=sta[i].dis;
nowprice=sta[i].price;
now=i;
maxdis = nowdis+cmax*davg;
leftdis=0;
}
}
if(i<=n&&sta[i].dis-sta[i-1].dis>cmax*davg){//若相邻站点距离过大,就输出你能到的最远距离
printf("The maximum travel distance = %.2f", maxdis);
return 0;
}
if(nowdis<d&&mp!=inf){//找到相对便宜的后,在当前站点加满,然后再到相对便宜的站点补充
totalPrice=totalPrice+nowprice*(cmax-leftdis/davg);
// cout<<" -"<<totalPrice<<"- \n";
leftdis=cmax*davg-(sta[ms].dis-nowdis);//用不完留着,下次可以少加点
nowdis=sta[ms].dis;
nowprice=mp;
// cout<<mp<<" "<<nowdis<<" ";
now=ms;
maxdis = nowdis+cmax*davg;
}
}
if(nowdis==d){
printf("%.2f", totalPrice);
}
return 0;
}