码迷,mamicode.com
首页 > 其他好文 > 详细

PAT1033.To Fill or Not to Fill

时间:2015-02-15 16:27:05      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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

思路:此题贪心较为复杂,策略主要是以下:
1:首先选择离自己最近的并且价格低于目前所在加油站的价格(必须可达)
2:若1无法找到,其次只能选择价格大于自己的并且尽量低的加油站(必须可达)
3:若1,2都无法找到,那么就进行输出距离
注意: 此题很长时间没有AC过,就是因为忽略了一种情况。当处于情况2的时候,选择价格大于自己的并且尽量低的加油站时,车在目前所在的加油站必须将油加满。因为便宜。切记。
技术分享
  1 #include <iostream> 
  2 #include <algorithm>
  3 using namespace std;
  4 
  5 struct Station
  6 {
  7     double P;
  8     double D;
  9 }Sta[510];
 10 
 11 bool cmp(struct Station A,struct Station B)
 12 {
 13     return A.D<B.D;
 14 }
 15 
 16 int main(int argc, char *argv[])
 17 {
 18     double Cmax,D,Davg;
 19     double cost=0;
 20     double distance=0;
 21     int N;
 22     scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&N);
 23     for(int i=0;i<N;i++)
 24     {
 25         scanf("%lf%lf",&Sta[i].P,&Sta[i].D);
 26     }
 27     sort(Sta,Sta+N,cmp);
 28     if(Sta[0].D!=0)
 29        printf("The maximum travel distance = 0.00\n");
 30     else
 31     {
 32         bool find=true;
 33         int begin=0; 
 34         //需要存储汽油容量
 35         double now=0;
 36         while(distance<D)
 37         {
 38            double min=Sta[begin].P;
 39             int pt=begin;
 40           //    cout <<"route:"<<begin<<" "<<cost<<endl;
 41             //寻找比自己少的加油站 
 42            for(int i=begin+1;i<N;i++)
 43               {
 44                  //low price, can arrive. 
 45               if(Sta[i].P<=min&&Sta[i].D-Sta[begin].D<=Cmax*Davg)
 46               {
 47                  min=Sta[i].P;
 48                  pt=i;
 49                  break;
 50               }
 51            }
 52             if(pt!=begin)
 53             {
 54                  //寻找到 
 55                  double need=(Sta[pt].D-Sta[begin].D)/Davg;
 56                  if(need>now)
 57                  {
 58                      cost+=(need-now)*Sta[begin].P;
 59                      now=0;
 60                    }
 61                    else
 62                    now-=need; 
 63               distance+=(Sta[pt].D-Sta[begin].D);
 64                 begin=pt;
 65                 now=0;
 66             }
 67             else
 68             {
 69                 //如果找不到.......
 70                 //寻找大于自己的并且价钱尽可能低的,另外可能直接到达目的地 
 71                 //检验是否能直接到达目的地
 72                 //检验是否能到达该加油站
 73                 if(Cmax*Davg+Sta[begin].D>=D)
 74                 {
 75                     cost+=((D-Sta[begin].D)/Davg-now)*Sta[begin].P;
 76                     break;
 77                 } 
 78                 if(begin+1==N)
 79                 {
 80                     int remain=D-Sta[begin].D;
 81                     if(remain>Cmax*Davg)
 82                     {
 83                         //can not arrive
 84                         find=false;
 85                         printf("The maximum travel distance = %.2f\n",distance+Cmax*Davg);                         
 86                     }
 87                     else
 88                     {
 89                         cost+=(remain/Davg-now)*Sta[begin].P;
 90                     }
 91                     break;     //quit
 92                 } 
 93                 min=Sta[begin+1].P;
 94                 pt=begin+1;
 95                 for(int i=begin+1;i<N;i++)
 96                 {
 97                     if(Sta[i].P<min&&Sta[i].D-Sta[begin].D<=Cmax*Davg)
 98                     {
 99                         pt=i;
100                         min=Sta[i].P;
101                     }
102                 }
103                //find 
104                  double need=(Sta[pt].D-Sta[begin].D)/Davg;
105                  //   if(need>now)
106                //   {
107                    cost+=(Cmax-now)*Sta[begin].P;//Cmax .....
108                    now=Cmax-need;
109                  //   }
110                 //      else
111            //         now-=need; 
112                  distance+=(Sta[pt].D-Sta[begin].D);
113                     begin=pt;                
114             }
115         }
116         if(find)        
117           printf("%.2f\n",cost);
118     }
119     return 0;
120 }
View Code

 

PAT1033.To Fill or Not to Fill

标签:

原文地址:http://www.cnblogs.com/GoFly/p/4293003.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!