https://www.cnblogs.com/cao-lei/p/7243599.html (original blog) reproduced (written by very good)
topic::
Algorithm to train traveler's budget
Next N rows, each row two real Di, Pi.
102.0 2.9
220.0 2.2
Think of the start point 0 stations, imagine the end stations of N + 1
from the starting point of the distance stations 0 is 0, the N + 1 from the starting point to the distance D1 stations
0th stations price is P, N + 1-0 stations price
can be calculated according to the liter tank capacity C and can travel distance D2 can fill up the tank can travel a maximum distance maxDis
:( no solution to determine when entering, find no solution as soon as possible)
the maximum distance if the distance is greater than fill up two gas stations can travel, then no solution
Solvable:
find the nearest and cheapest gas from a gas station at the current location:
1. If you can find a
(1) if they can reach a refueling, then added just to reach the cheapest gas can
(2) If one can not be reached, then the first to fill the tank, the gas station before reaching the maximum distance of travel, plus just to travel to the cheap gas station
2. If not found, then the top up, the maximum distance to travel before that gas station, continue to look for
attention: the end of gas station price is zero, the last time just added fuel to reach the finish line
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 5 #define MAX_NUM 1001 6 7 int main() 8 { 9 int N; 10 double D1, C, D2, P; 11 scanf("%lf%lf%lf%lf%d", &D1, &C, &D2, &P, &N); 12 13 // double * distance = new double[N+5]; //加油站i到起点的距离 14 // double * price = new double[N+5]; //The price of oil 15 Double Distance [to MAX_NUM]; 16 Double . Price [to MAX_NUM]; . 17 18 is Distance [ 0 ] = 0 ; // from the i-th point from the recording starting point . 19 . Price [ 0 ] = P; // origin oil 20 is Distance [N + . 1 ] = Dl; // end of the last gas station 21 is . price [N + . 1 ] = 0 ; // end price 22 is 23 is Double Total = 0 ; //Cost 24- Double Surplus = 0 ; // remaining oil to the i-th gas station when the 25 Double maxDis = C * D2; // fill up exercise maximum distance of 26 BOOL Flag = to true ; // whether a solution, The default solvable 27 28 for ( int I = . 1 ; I <= N; I ++ ) 29 { 30 Scanf ( " % LF% LF " , & Distance [I], & . price [I]); 31 is IF (Distance [I] - Distance [I- . 1 ]> maxDis) //If the distance is greater than two stations can fill up exercise distance, no solution of 32 { 33 is In Flag = to false ; // no solution 34 is } 35 } 36 37 [ IF (In Flag!) // no solution 38 is { 39 the printf ( " No Solution \ n- " ); 40 return 0 ; 41 is } 42 is 43 is / * 44 is I: number of current stations 45 J: cheaper than their next stations numbered 46 is * / 47 for ( int= I 0 , j; I <= N; j = I) // Upon arrival j, j assigned to the i, recycled, know to reach the end 48 { 49 for (j = I + . 1 ; j <= N + . 1 ; J ++) // a start looking for cheaper than its i from the stations 50 { 51 is IF (Distance [J] - Distance [i]> maxDis) // if it is unable to exercise to be cheaper than gas station 52 { 53 J,; // you can fill up the first sail will have the maximum driving distance before the gas station 54 is BREAK ; 55 } 56 is IF (. price [J] <=. price [I]) //Get now cheaper than gas station where the gas station 57 is { 58 BREAK ; 59 } 60 } 61 is 62 is / * 63 is looking for the nearest and cheapest gas from a gas station at the current location: 64 1 If you can find the 65 ( 1) If you can reach a refueling, then added just to reach the cheapest gas can be 66 (2) if one can not be reached, then the first to fill the tank, the gas station before reaching the maximum distance of travel, plus just to cheap travel to that gas station 67 2 If not found, then the top up, the gas station before traveling to the maximum distance, continue to look for 68 * / 69 70 IF (. price [J] <=. price [i]) // Under (1) 71 { 72 + = Total ((Distance [j] - Distance [I]) / D2 of - Surplus). price * [I]; // Save can exercise just to the point j 73 is Surplus = 0 ; // remaining oil 74 } 75 the else // belonging to (2) or 2 76 { 77 Total + = (C - Surplus). price * [I]; // fill the tank 78 Surplus = C - (Distance [J] - Distance [I]) / D2 of; / / time point j to the remaining oil 79 } 80 } 81 82 the printf ( " % .2lf \ n- ", total); 83 84 return 0; 85 }
Although it is copied, but the key you want to understand! ! ! ! ! ! ! !