PAT1033.To Fill or Not to Fill

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 }

时间: 2024-08-27 05:59:37

PAT1033.To Fill or Not to Fill的相关文章

pat1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Guochuan 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 fin

PAT 1033. To Fill or Not to Fill (25)(贪心)

解题思路: 其实题目本身不难,难就难在对贪心策略的选取 在每一个加油点应该做这样的判断: 1.首先计算从本加油点做多可能达到的加油点 2.如果有可达加油点的话: 2.1 寻找有无比当前加油点油价便宜的加油点,如果有的话就跑到该便宜的加油点,油量加到支持到该加油点即可 2.2 如果没有比当前加油点更便宜的加油点的话,又分两种情况: 2.2.1.如果本次加油可能直接到终点,就加可以到达终点的油量 2.2.2.否则的话,加满 3.如果没有可达加油点的话: 3.1 看是否可以直接达到终点 3.2 不能达

九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: 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 fro

【贪心】PAT 1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Guochuan 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 fin

PAT 1033 To Fill or Not to Fill[dp]

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 gi

1033 To Fill or Not to Fill (25 分)

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 g

PAT 1033. To Fill or Not to Fill

#include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> using namespace std; int main() { int N; double mcap, dist, davg; scanf("%lf%lf%lf%d", &mcap, &dist, &davg, &N); double price, id

PAT 1004 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

PAT (Advanced Level) 1033. To Fill or Not to Fill (25)

贪心.注意x=0处没有加油站的情况. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct X { double cost, x, v; int