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, printThe maximum travel distance = X
WhereX
Is the maximum possible distance the car can run, accurate up to 2 decimal places.
Knowledge point:
Greedy Algorithm
Ideas:
For each gas station:
- There are cheaper gas stations in the available range:
- This site is just enough to open to the next gas station. the next gas station is the latest one that is cheaper than this station.
- There is no cheaper gas station in the available range:
- This site is filled with oil, and the next gas station is the cheapest and farthest
- There is no gas station in the available range:
- How far can I open it when the site is filled with oil?
- Special cases:
- D = 0 cost 0
- No gas station at the start
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. dist <B. dist; 16} 17 18 int main (INT argc, char * argv []) {19 20 scanf ("% lf % d", & Cmax, & D, & davg, & N); 21 for (INT I = 0; I <n; I ++) {22 scanf ("% 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) {// In special cases, the start point is 30 printf ("0.00 \ n"); 31 return 0; 32} 33 If (GS [0]. dist! = 0) {// no gas station 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) {// you can find the latest 51 next_locate = I; 52 cheaper = true; 53 break; 54} 55 if (GS [I]. price <= cheapest) {// no cheaper. Find the cheapest 56 cheapest = GS [I]. price; 57 next_locate = I; 58} 59} 60 if (next_locate =-1) {// 61 longestd = GS [locate] that cannot be reached. 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 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 300
Sample output 1:
749.17
Sample input 2:
50 1300 12 27.10 07.00 600
Sample Output 2:
The maximum travel distance = 1200.00
1033 to fill or not to fill