PAT(贪心)——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 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<iostream>
#include<algorithm>
using namespace std;
#define MAXN 1005
struct node{
double distance;
double price;
}A[MAXN];
bool cmp(node a,node b){
return a.distance<b.distance;
}
double capacity,average,des;
int n;
int main()
{
scanf("%lf%lf%lf%d",&capacity,&des,&average,&n);
for(int i=0;i<n;i++)
scanf("%lf%lf",&A[i].price,&A[i].distance);
A[n]={des,0.0};
bool flag=true;
double all_price=0.0;
int now_station=0;
sort(A,A+n+1,cmp);
if(A[0].distance>0){
printf("The maximum travel distance = 0.00");
return 0;
}
while(flag==true&&now_station!=n){
int i,next_station=-1;
double min=99999999;
for(i=now_station+1;A[i].distance-A[now_station].distance<=capacity*average&&i<=n;i++){
if(A[i].price<A[now_station].price){
next_station=i;
break;
}else if(A[i].price<min){
min=A[i].price;
next_station=i;
}
}
if(next_station==-1)
flag=false;
else{
all_price+=A[now_station].price*(A[next_station].distance-A[now_station].distance)/average;
now_station=next_station;
}
}
if(flag==false){
printf("The maximum travel distance = %.2f",A[now_station].distance+capacity*average);
}else
printf("%.2f",all_price);
return 0;
}