PAT_A1033#To Fill or Not to Fill

Source:

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

Description:

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​​ (≤), the distance between this station and Hangzhou, for ,. 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

Keys:

Code:

 1 /*
 2 Data: 2019-07-23 19:26:36
 3 Problem: PAT_A1033#To Fill or Not to Fill
 4 AC: 56:21
 5 
 6 题目大意:
 7 旅途中各个加油站的价格不同,用最少的钱到达目的地;
 8 或能够到达的最远距离
 9 输入:
10 第一行给出,油箱最大容量Cmax<=100,目的地距离D<=3e4,单位油量行驶距离Davg<=20,加油站数量N<=500
11 接下来N行,价格p,距起点距离d
12 
13 基本思路:
14 秉承各地加油站能少花钱则少花钱原则
15 预设:目的地油价=0,起始油量=0
16 前方油价低于当前油价,直接前往
17 (此时油量一定是不够的,否则上一轮前进时,就会到达该站,因此加适量油即可)
18 否则前往前方油价最低的加油站
19 (能够到达则直接去;不能到达则加满油,如果加恰好的油,那么到前方一定会加油,花的钱一定比现在多,所以现在加满)
20 */
21 #include<cstdio>
22 #include<algorithm>
23 using namespace std;
24 const int M=550;
25 struct node
26 {
27     double p,d;
28 }gas[M];
29 
30 bool cmp(const node &a, const node &b)
31 {
32     return a.d < b.d;
33 }
34 
35 int main()
36 {
37 #ifdef    ONLINE_JUDGE
38 #else
39     freopen("Test.txt", "r", stdin);
40 #endif
41 
42     double Cmax,D,Davg,bill=0,tank=0;
43     int n,now=0;
44     scanf("%lf%lf%lf%d", &Cmax,&D,&Davg,&n);
45     for(int i=0; i<n; i++)
46         scanf("%lf %lf", &gas[i].p, &gas[i].d);
47     sort(gas,gas+n,cmp);
48     gas[n] = node{0,D};
49     if(gas[0].d != 0)
50     {
51         printf("The maximum travel distance = 0.00");
52         n=0;
53     }
54     for(int i=0; i<n; i++)
55     {
56         if(gas[i].d+Cmax*Davg < gas[i+1].d)
57         {
58             printf("The maximum travel distance = %.2f", gas[i].d+Cmax*Davg);
59             n=0;break;
60         }
61     }
62     while(now<n)
63     {
64         int pos=now+1,next=now+1;
65         double mprice=gas[now].p;
66         while(gas[now].d+Cmax*Davg>=gas[pos].d)
67         {
68             if(gas[pos].p < mprice)
69             {
70                 mprice = gas[pos].p;
71                 next = pos;
72                 break;
73             }
74             else if(gas[pos].p < gas[next].p)
75                 next = pos;
76             pos++;
77         }
78         tank -= (gas[next].d-gas[now].d)/Davg;
79         if(mprice < gas[now].p)
80         {
81             bill += (-1)*tank*gas[now].p;
82             tank=0;
83         }
84         else if(tank < 0)
85         {
86             bill += (Cmax-tank-(gas[next].d-gas[now].d)/Davg)*gas[now].p;
87             tank = Cmax - (gas[next].d-gas[now].d)/Davg;
88         }
89         now = next;
90     }
91     if(n)
92         printf("%.2f", bill);
93 
94     return 0;
95 }

 

posted @ 2019-07-24 20:08  林東雨  阅读(224)  评论(0编辑  收藏  举报