PAT A1033. To Fill or Not to Fill (25)

来源:互联网 发布:漫威电影补剧顺序 知乎 编辑:程序博客网 时间:2024/06/09 13:44

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

mmp,这题真是写跪了,知道是贪心算法,就是懒得翻书,硬着头皮自己写的,有几个易错点:
一、while(station[i].distance-length<=Davg*Cmax)跳出循环的有两种情况:①没找到更低价的加油站;②没找到下一个加油站,想了很久用了一个cnt计数器进行判断,别的办法我还没有想到。
二、在找不到更便宜的加油站时,选择本地的加油站加满,油价需要变更为本地油价,此处易出错。

//每到一个站时,加油前比较,加多少油时,能刚好抵达价格较低的加油站,若无较低的加油站,在本地加满//(若距目的地近则不加满),若加满后仍不能抵达下一站则返回最远距离。#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>using namespace std;const int maxn=510;struct gas{    double price;    int distance;}station[maxn];bool cmp(gas a,gas b){    return a.distance<b.distance;}int main(){//  freopen("in.txt","r",stdin);//  freopen("out.txt","w",stdout);     int Cmax,D,Davg,N;    int i,j,cnt;    double amount;//总开销     double money;//记录某时刻的油价     double length;//当前位置     double remain;//剩余油量     while(cin>>Cmax>>D>>Davg>>N){        amount=0.0;        length=0.0;        remain=0.0;        i=1;        cnt=0;        cin.get();        for(int k=0;k<N;k++){            cin>>station[k].price>>station[k].distance;             }           sort(station,station+N,cmp);//按距离从小到大排序         station[N].distance=D;        station[N].price=0.0;//将第N个站标记为目的地         if(station[0].distance!=0){            printf("The maximum travel distance = 0.00\n");        }        else{            money=station[0].price;            while(i<=N){                if(station[i].distance-length>Davg*Cmax){                        length+=Davg*Cmax;                        printf("The maximum travel distance = %.2lf\n",length);                         return 0;                                           }//这三句在后面出现过,确实重复了,但如果不加的话,测试第二实例时会陷入两个while的死循环,提示超时扣三分                while(station[i].distance-length<=Davg*Cmax){                    if(++cnt&&station[i].price<=money){                        amount+=money*(station[i].distance-length)/Davg-remain*money;                        length=station[i].distance;                        money=station[i].price;                        remain=0.0;                        if(fabs(money-0.0)<1e-6){                            printf("%.2lf\n",amount);                            return 0;                        }                        ++i;                        cnt=0;                        continue;                    }//有便宜的行进,无便宜的不动                    ++i;                                if(station[i].distance-length>Davg*Cmax){                        if(cnt==0){                            length+=Davg*Cmax;                            printf("The maximum travel distance = %.2lf\n",length);                             return 0;                                                   }                        else{                                                       money=station[i-1-cnt].price;                            amount+=money*(Cmax-remain);                            remain=Cmax-(station[i-1].distance-length)/Davg;                            length=station[i-1].distance;//走到第i-1站                             money=station[i-1].price;//必须要加上这一步,否则从1000到1250时,费用将从7.00计算出错                             cnt=0;                        }                    }//油程内,无法找到更便宜的                 }               }        }    }    return 0;}
原创粉丝点击