题目地址

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: C
​max
​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D
​avg
​​ (≤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: P
​i
​​ , the unit gas price, and D
​i
​​ (≤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

作者: ZHANG, Guochuan
单位: 浙江大学
时间限制: 200 ms
内存限制: 64 MB

#include<iostream>
#include<algorithm>
using namespace std;
struct station{
    double distance,unit_price;
}s[10010];
const int inf=100000000;
bool cmp(station a,station b){
    if(a.distance!=b.distance) return a.distance<b.distance;
    // else if(a.unit_price!=b.unit_price) return a.unit_price<b.unit_price;
}
int main(){
    double Cmax,d,Davg;int n;
    cin>>Cmax>>d>>Davg>>n;
    for(int i=0;i<n;i++) cin>>s[i].unit_price>>s[i].distance; s[n].unit_price=0;s[n].distance=d;
    sort(s,s+n+1,cmp);
    double max_dis=Cmax*Davg,ans_dis=0,ans_price=0,now_tank=0;int now=0;
    if(s[0].distance){
        printf("The maximum travel distance = 0.00\n");return 0;        //0.00
    }
    while(now<n){        //n是终点; 
        int next=now+1,goal_idx=-1;double goal_price=inf;        //存600米内第一个小于now油价或最小油价,当前已处在一个车站; next+1;
        while(next<=n&&s[next].distance-s[now].distance<=max_dis){        //most important 2; <=
            if(goal_price>s[next].unit_price) {
                goal_idx=next;goal_price=s[next].unit_price;
            }
            if(s[next].unit_price<s[now].unit_price) {
                goal_idx=next;goal_price=s[next].unit_price;break;
            }
            next++;
        }
        if(goal_idx==-1){        //找不到车站; 
            break;
        }
        double need_gas= (s[goal_idx].distance-s[now].distance)/Davg;
        if(goal_price<s[now].unit_price){        //如果目标站更便宜,只买足够油到达目标站, 
            if(now_tank<need_gas) {                    //当前油量小于需要油量; 
                ans_price+=s[now].unit_price*(need_gas-now_tank);        //只加能到目标站的油, 
                now_tank=0;
            }
            else{            //否则到目标站加油; 
                now_tank-=need_gas;
            } 
        }
        else{                //目标站贵 ,此战加满; 
            ans_price+=(Cmax-now_tank)*s[now].unit_price;
            now_tank=Cmax-need_gas;
        }
        now=goal_idx;
        
    }
    if(now==n){         
            printf("%.2lf",ans_price);
        }
        else 
        printf("The maximum travel distance = %.2lf\n",s[now].distance+max_dis);return 0;    
}