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
思路:贪心,如果当前加油站是所能到达范围内油价最低的,那么就在这里加满,然后去范围内次低的加油站;如果当前加油站不是范围内油价最低的加油站,那就在当前加油站加到够跑到油价最低加油站的油
#include <bits/stdc++.h>
using namespace std;
struct node
{
double price,length;
};
bool cmp(node a,node b) {return a.length<b.length;} //加油站按距离排序
int main()
{
double cmax,d,dvg;
int n;
vector<node> stations;
scanf("%lf %lf %lf %d",&cmax,&d,&dvg,&n);
for(int i=0;i<n;i++)
{
node temp;
if(i==0) //把终点看成一个油价为0,距离最大的加油站
{
temp.price=0;
temp.length=d;
stations.push_back(temp);
}
scanf("%lf %lf",&temp.price,&temp.length);
stations.push_back(temp);
}
sort(stations.begin(),stations.end(),cmp);
if(stations[0].length!=0) //对于测试点2,如果第一个加油站不在起点,直接返回0
{
printf("The maximum travel distance = 0.00");
return 0;
}
int pre=0,st=0; //pre为上次位置,st为当前位置
double sumcost=0,tempcmax=0,maxlength=cmax*dvg,sumlength=0;
bool flag=true;
while(st!=stations.size()-1)
{
tempcmax-=(stations[st].length-stations[pre].length)/dvg; //每次消耗的油量
double minprice=stations[st].price;
int pos1=-1,pos2=-1;
for(int i=st;(i<stations.size())&&(stations[i].length-stations[st].length<=maxlength);i++) //去找范围内最低油价加油站
{
if(stations[i].price<=minprice)
{
minprice=stations[i].price;
pos1=i;
if(i!=st) break;
}
}
if(pos1==st) //如果本身就是范围内最低油价加油站,找范围内次低加油站,跑到那里去
{
double secprice=stations[st+1].price;
for(int i=st+1;(i<stations.size())&&(stations[i].length-stations[st].length<=maxlength);i++)
{
if(stations[i].price<=secprice)
{
secprice=stations[i].price;
pos2=i;
}
}
}
if(pos1==st&&pos2==-1) //下一站不可达
{
sumcost+=(cmax-tempcmax)*stations[st].price;
sumlength+=maxlength;
tempcmax=cmax;
flag=false;
break;
}
else if(pos1==st&&pos2!=-1) //当前为最低站,且次低站可达,直接加满,然后到次低站
{
sumcost+=(cmax-tempcmax)*stations[st].price;
sumlength+=(stations[pos2].length-stations[st].length);
tempcmax=cmax;
pre=st;
st=pos2;
}
else if(pos1!=st) //如果当前站不是最低站,那么去距离最近的油价低的站
{
double templength=stations[pos1].length-stations[st].length;
if(tempcmax*dvg<templength) //如果当前油箱的油到不了目的站就需要加油
{
sumcost+=(templength/dvg-tempcmax)*stations[st].price;
sumlength+=templength;
tempcmax=templength/dvg;
pre=st;
st=pos1;
}
else //如果当前油量能到下一目的站就不用加油
{
maxlength=templength;
pre=st;
st=pos1;
}
}
}
if(flag==false) printf("The maximum travel distance = %.2f",sumlength);
else printf("%.2f",sumcost);
return 0;
}