1. 程式人生 > >1033 To Fill or Not to Fill

1033 To Fill or Not to Fill

erl rom strong put diff 針對 下一個 ont \n

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\n");
31         return 0;
32     }
33     if(GS[0].dist!=0){ // 特殊情況 起點沒有加油站
34         printf("The maximum travel distance = 0.00\n");
35         return 0;
36     }
37 
38     //printf("%d\n",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\n",GS[locate].dist);
63             printf("The maximum travel distance = %.2f\n",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\n",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