1033 To Fill or Not to Fill

Posted lokwongho

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1033 To Fill or Not to Fill相关的知识,希望对你有一定的参考价值。

PAT A 1033 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: 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.

知识点:

贪心算法

思路:

针对所处的每一个加油站,有如下情况:

  • 在可以开到的范围内,有更便宜的加油站:
    • 本站加到刚好够开到下一个加油站;下一个加油站是最近的一个比本站便宜的
  • 在可以开到的范围内,没有更便宜的加油站:
    • 本站加满油,下一个加油站是最便宜且,最远的
  • 在可以开到的范围内,没有加油站:
    • 在本站加满油,能开多远开多远
  • 特殊情况:
    • d == 0 花费为0
    • 起点没有加油站 最远走0
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 const int maxn = 500;
 5 
 6 double cmax,d,davg;
 7 int n;
 8 struct GSType{
 9     double dist;
10     double price;    
11 };
12 struct GSType GS[maxn];
13 
14 bool cmp(struct GSType a, struct GSType b){
15     return a.dist<b.dist;
16 }
17 
18 int main(int argc, char *argv[]) {
19     
20     scanf("%lf %lf %lf %d",&cmax,&d,&davg,&n);
21     for(int i=0;i<n;i++){
22         scanf("%lf %lf",&GS[i].price,&GS[i].dist);
23     }
24     GS[n].dist = d;
25     GS[n].price = 0.0;
26     sort(GS, GS+n, cmp);
27 
28 
29     if(d==0){ // 特殊情况 起点即终点
30         printf("0.00
");
31         return 0;
32     }
33     if(GS[0].dist!=0){ // 特殊情况 起点没有加油站
34         printf("The maximum travel distance = 0.00
");
35         return 0;
36     }
37 
38     //printf("%d
",GS[0].dist);
39     double tol_w = 0.0;
40     double Df = cmax*davg;
41     double LongestD = 0.0; 
42     double remain = 0.0;
43     
44     int locate = 0;
45     while(locate!=n){
46         double cheapest = 99999999.9;
47         int next_locate = -1;
48         bool cheaper = false;
49         for(int i=locate+1;i<=n&&(GS[locate].dist)<GS[i].dist&&GS[i].dist<=(GS[locate].dist+Df);i++){
50             if(GS[i].price<GS[locate].price){ //有便宜的,找最近的
51                 next_locate = i;
52                 cheaper = true;
53                 break;
54             }
55             if(GS[i].price<=cheapest){ // 没有比便宜的,找最便宜的
56                 cheapest = GS[i].price;
57                 next_locate = i;
58             }
59         }
60         if(next_locate == -1){ // 没有能到的
61             LongestD = GS[locate].dist+cmax*davg;
62             //printf("%f
",GS[locate].dist);
63             printf("The maximum travel distance = %.2f
",LongestD);
64             return 0;
65         }
66         if(cheaper){
67             if((GS[next_locate].dist-GS[locate].dist)/davg>remain){
68                 tol_w += ((GS[next_locate].dist-GS[locate].dist)/davg-remain)*GS[locate].price;
69                 remain = 0;
70             }else{
71                 remain -= (GS[next_locate].dist-GS[locate].dist)/davg;
72             }
73         }else{
74             tol_w += (cmax-remain)*GS[locate].price;
75             remain = cmax-(GS[next_locate].dist-GS[locate].dist)/davg;
76         }
77         locate = next_locate;
78     }
79     printf("%.2f
",tol_w);
80 }

 

 

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

 

以上是关于1033 To Fill or Not to Fill的主要内容,如果未能解决你的问题,请参考以下文章

PAT 1033. To Fill or Not to Fill

1033 To Fill or Not to Fill (25 分)

1033. To Fill or Not to Fill (25)

1033 to fill or not to fill

PAT 1033 To Fill or Not to Fill[dp]

1033. To Fill or Not to Fill (25)