PAT 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 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 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 struct GasStation
 8 {
 9     double price;
10     double dst;
11     double fill;
12 };
13 
14 GasStation station[500];
15 int maxTank, totalDst, stationNum, avg;
16 
17 void GasConsume(double initialGas, int start, int end)
18 {
19     if (start == end)
20         return;
21     //find the station with min price
22     double minprice = station[start].price;
23     int min = start;
24     for (int i = start + 1; i < end; i++)
25     {
26         if (station[i].price <= minprice)
27         {
28             minprice = station[i].price;
29             min = i;
30         }
31     }
32     double length1 = station[min].dst - station[start].dst;
33     double length2 = station[end].dst - station[min].dst;
34     double maxConsume1 = length1 / avg, maxConsume2 = length2 / avg;
35     if (maxConsume1 >= initialGas && maxConsume2 >= maxTank)
36         station[min].fill = maxTank;
37     else if (maxConsume1 < initialGas && maxConsume2 >= maxTank)
38         station[min].fill = maxTank - (initialGas - maxConsume1);
39     else if (maxConsume1 > initialGas && maxConsume2 < maxTank)
40         station[min].fill = maxConsume2;
41     else if (maxConsume1 + maxConsume2 > initialGas)
42         station[min].fill = maxConsume2 - (initialGas - maxConsume1);
43     else
44         station[min].fill = 0;
45 
46     GasConsume(initialGas, start, min);
47     double totalFill = 0;
48     for (int i = start; i <= min; i++)
49         totalFill += station[i].fill;
50     double init = initialGas + totalFill - (station[min + 1].dst - station[start].dst) / avg;
51     GasConsume(init, min+1, end);
52 
53 }
54 
55 bool cmp(const GasStation& s1, const GasStation& s2)
56 {
57     return s1.dst < s2.dst;
58 }
59 
60 int main()
61 {
62 
63     cin >> maxTank >> totalDst >> avg >> stationNum;
64 
65     for (int i = 0; i < stationNum; i++)
66         cin >> station[i].price >> station[i].dst;
67     station[stationNum].dst = totalDst;
68     sort(station, station + stationNum, cmp);
69 
70     if (station[0].dst != 0)
71     {
72         cout << "The maximum travel distance = 0.00";
73         return 0;
74     }
75     for (int i = 1; i <= stationNum; i++)
76     {
77         if (station[i].dst - station[i - 1].dst > maxTank*avg)
78         {
79         
80             cout << "The maximum travel distance = ";
81             printf("%.2f", station[i - 1].dst + maxTank*avg);
82             return 0;
83         }
84     }
85     GasConsume(0, 0, stationNum);
86     double totalCost = 0, total=0;
87     for (int i = 0; i < stationNum; i++)
88         totalCost += station[i].fill*station[i].price;
89 
90     printf("%.2f", totalCost);
91 }

 

posted @ 2015-08-24 21:46  JackWang822  阅读(225)  评论(0编辑  收藏  举报