- 题目描述
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<stdio.h>
#include<stdlib.h>//快排头文件
int cmp(const void*a,const void*b){
if(((int *)a)[1] - ((int *)b)[1]>0)
return 1;
else
return -1;
}
int main(){
int C,D,Dis,N;
int i,j;
float gasv=0.0;
float summoney=0.0;
int curstation=0,nextstation=-1;
float gas[502][2];
scanf("%d %d %d %d",&C,&D,&Dis,&N);
for(i=0;i<N;i++){
scanf("%f %f",&gas[i][0],&gas[i][1]);
}
qsort(gas,N,sizeof(gas[0]),cmp);
int longest=C*Dis;
for(i=0;gas[i][1]<D;i++){
//1有更便宜的
//2选择相对最便宜的,并在之前此处加满
//3没有加油站
if(i==0&&gas[i][1]>0){
printf("The maximum travel distance = 0.00\n");
break;
}
else if(i==N&&D-gas[curstation][1]<=C*Dis){
summoney+=((D-gas[curstation][1])/(float)Dis-gasv)*gas[curstation][0];
printf("%.2f\n",summoney);
break;
}
else if(i==N&&D-gas[curstation][1]>C*Dis&&nextstation==-1){
printf("The maximum travel distance = %.2f",(float)gas[curstation][1]+longest);
break;
}
else if(i==N&&D-gas[curstation][1]>C*Dis&&nextstation!=-1){
summoney+=(C-gasv)*gas[curstation][0];
gasv=(float)C-(float)(gas[nextstation][1]-gas[curstation][1])/(float)Dis;
curstation=nextstation;
i=nextstation;
nextstation=-1;
}
else if(gas[i][1]-gas[curstation][1]<=longest){
//2当前加油站价钱更便宜
if(gas[i][0]<=gas[curstation][0]){
summoney+=((gas[i][1]-gas[curstation][1])/(float)Dis-gasv)*gas[curstation][0];
gasv=0.0;
gas[curstation][1]=gas[i][1];
curstation=i;
nextstation=-1;
}
else if(gas[i][0]>gas[curstation][0]){
if(nextstation==-1||gas[i][0]<=gas[nextstation][0])
nextstation=i;
}
}
//相对便宜的
else if(gas[i][1]-gas[curstation][1]>longest&&nextstation!=-1){
//加满油
summoney+=(C-gasv)*gas[curstation][0];
gasv=(float)C-(float)(gas[nextstation][1]-gas[curstation][1])/(float)Dis;
curstation=nextstation;
i=nextstation;
nextstation=-1;
}
//没有加油站不可能
else if(gas[i][1]-gas[curstation][1]>longest&&nextstation==-1){
printf("The maximum travel distance = %.2f\n",(float)gas[curstation][1]+longest);
break;
}
}
return 0;
}
结果
第四个用例没有通过,代码有不周全的地方还请大家不吝赐教。