1033. To Fill or Not to Fill (25)
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
本题是个行车加油找最小花费问题,解法是贪心。
#include <cstdio>
#include <cstdlib>
#include <climits>
typedef struct{
double price;
double dist;
}STATION;
int cmp(const void *a,const void *b){
int ret;
STATION *pa = (STATION *)a;
STATION *pb = (STATION *)b;
return pa->dist-pb->dist;
}
STATION st[501];
int main(){
double cmax,dist,davg,currgas,res,minprice;
int n,i,j,pos;
scanf("%lf %lf %lf %d",&cmax,&dist,&davg,&n);
for(i=0;i<n;++i){
scanf("%lf %lf",&st[i].price,&st[i].dist);
}
st[n].price = 0;
st[n].dist = dist;
qsort(st,n,sizeof(STATION),cmp);
if(st[0].dist>0){
printf("The maximum travel distance = 0.00\n");
return 0;
}
currgas = 0.0;
double limit = cmax * davg;
res = 0.0;
for(i=0;i<n;){
if(st[i+1].dist-st[i].dist>limit){//can't reach the destination
printf("The maximum travel distance = %.2lf\n",st[i].dist + limit);
return 0;
}
pos = i;
minprice = st[i].price;
//在当前剩余油所能到的范围内找比现在站油价便宜的站中最便宜的站,可以直接跑过去,而不需要加油
for(j=i+1;j<=n && st[j].dist-st[i].dist<=currgas*davg;++j){
if(st[j].price<minprice){
minprice = st[j].price;
pos = j;
}
}
if(pos!=i){//现在有油,而且在油能到范围内找到了比现在站更便宜而且是价格最便宜的油站(不加油能到),就直接开过去,不加油,到那再加
currgas -= ((st[pos].dist-st[i].dist)/davg);
i = pos;
continue;
}
//没油或者是有油但是油所能到的范围内没有比现在站价格更便宜的站
//此时必须在此站加油了,关键是计算要加多少油
//要找距离次油站最近的比该油站价格便宜的站,然后到了那站再贪心
for(j=i+1;j<=n && st[j].dist-st[i].dist<=limit;++j){
if(st[j].price<minprice){
minprice = st[j].price;
pos = j;
break;
}
}
//现在没油或现油箱所能到的油站价格都比现在油站贵:要去最近的比现油站便宜的油站(不加油到不了),得在现油站加上刚好满足的油量
if(pos!=i){
res += ((st[pos].dist-st[i].dist)/davg-currgas)*st[i].price;
currgas = 0;//刚开始这里忘记将currgas清零,一直结果不对
i = pos;
continue;
}
minprice = INT_MAX;
//现在有油或没油,且油箱所能到的所有站价格都比现油站贵
//所以在油箱所能到的所有油站中选择一个最便宜的把车加满油开过去,然后以该站为起点继续贪心即可
for(j=i+1;j<=n && st[j].dist-st[i].dist<=limit;++j){
if(st[j].price<minprice){
minprice = st[j].price;
pos = j;
}
}
res += (cmax-currgas)*st[i].price;
currgas = cmax-(st[pos].dist-st[i].dist)/davg;
i = pos;
}
printf("%.2lf\n",res);
return 0;
}