题目描述:
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.
输入:
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.
输出:
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
50 1300 12 2
7.10 0
7.00 600
样例输出:
749.17
The maximum travel distance = 1200.00
算法思想:
首先,将各个加油站按距离从近到远排序;
if 最近的加油站距离不为零,则输出最远距离0,结束;
else if 最近的加油站距离为零:
for 每个加油站(从第1个到第N-1个):
if 下一个加油站不可达,则计算并输出最远距离,结束;
if 最大油箱距离内的加油站最便宜的比当前加油站便宜,则在该站加够到该站的油即可;
else
if 当前加油站可达终点,则在该站加够到终点的油即可,计算并输出总开销,结束
else 则在当前站加满油;
对于第N个加油站:
if 该加油站可达终点,则计算并输出总开销,结束;
else 则计算并输出最远距离,结束;
算法实现:
#include <stdio.h>
typedef struct node{
double price,distance;
}node;
int cmp(node *a,node *b){
return a->distance>b->distance;
}
int main()
{
node data[501];
double Cmax,D,Dvag;
int N;
while(scanf("%lf %lf %lf %d",&Cmax,&D,&Dvag,&N)!=EOF){
for(int i=0;i<N;i++)
scanf("%lf %lf",&data[i].price,&data[i].distance);
qsort(data,N,sizeof(node),cmp);
int ret=0;//本组样例是否已完成输出
double remain=0;//剩余油量
double cost=0;//当前花销
if(data[0].distance!=0){
printf("The maximum travel distance = 0.00\n");
ret=1;
}else{
int goMax=Cmax*Dvag;
for(int i=0;i<N-1;i++){
if(data[i+1].distance>data[i].distance+goMax){
printf("The maximum travel distance = %.2f\n",data[i].distance+goMax);
ret=1;
break;
}
int min=i,j=i+1;
while(data[j].distance<=data[i].distance+goMax){
if(data[j].price<data[min].price)
min=j;
j++;
}
if(min!=i){
int needOil=(data[min].distance-data[i].distance)/Dvag;
if(remain<needOil){
cost+=(needOil-remain)*data[i].price;
remain=0;
}else{
remain-=needOil;
}
i+=(min-i-1);
}else{
if(data[i].distance+goMax>=D){
int needOil=(D-data[i].distance)/Dvag;
if(remain<needOil)
cost+=(needOil-remain)*data[i].price;
break;
}else{
cost+=(Cmax-remain)*data[i].price;
int needOil=(data[i+1].distance-data[i].distance)/Dvag;
remain=Cmax-needOil;
}
}
}
if(data[N-1].distance+goMax>=D){
int needOil=(D-data[N-1].distance)/Dvag;
if(remain<needOil)
cost+=(needOil-remain)*data[N-1].price;
}else{
printf("The maximum travel distance = %.2f\n",data[N-1].distance+goMax);
ret=1;
}
}
if(ret==0)
printf("%.2f\n",cost);
}
return 0;
}
投入了4个小时,还没能完全做对,要说收获,应该就是探索和专注带来的宁静吧~