PAT-A's last question, finally made...
It is greedy and obtains global optimization through local optimization.
1. Sort gas stations in ascending order of distance
2. Record the index of the current gas station, the existing gasoline, and the cost. Traverse all the gas stations that the station can arrive.
3. There are two situations in traversal:
1) if the oil price is lower than the index, click next:
Jump to the station immediately (you may need to refuel at this time) and stop traversing-because even if you want to reach the station after next, you can purchase cheaper gasoline at next.
2) If the oil price is lower than the index, select the station with the lowest oil price among all stations as next:
In this case, whether the index can be used to reach the destination. If yes, you can use the break directly without worrying about next. If not, it will be filled with oil and travel to the next station.
4. Test Point 2 where the maximum distance is 0-that is, there is no gas station at the start point.
Code:
# Include <iostream> # include <iomanip> # include <vector> # include <algorithm> using namespace std; struct Station {double price; int dis; Station (double p, int d): price (p), dis (d) {} friend bool operator <(const Station & a, const Station & B) {return. dis <B. dis ;}}; int main () {vector <Station> station; double cmax, dest, davg, p; int n, d; int index = 0; // index indicate the station where they are. double cost = 0, gas = 0; cin> cmax> dest> davg> n; for (int I = 0; I <n; ++ I) {cin> p> d; station. push_back (Station (p, d);} sort (station. begin (), station. end (); if (station [0]. dis! = 0) {cout <"The maximum travel distance = 0.00" <endl; return 0 ;}while (true) {// select The next station double min_price = 2100000000; int next =-1; bool find_cheaper = false; for (int I = index + 1; I <n & station [I]. dis <dest & station [I]. dis <= station [index]. dis + cmax * davg; ++ I) {if (station [I]. price <= station [index]. price) {find_cheaper = true; next = I; break;} else if (station [I]. price <min_price) {min_price = s Tation [I]. price; next = I ;}// select the fuel filling method if (find_cheaper = true) {if (station [next]. dis-station [index]. dis> gas * davg) // The oil cannot reach the station {cost + = (station [next]. dis-station [index]. dis)/davg-gas) * station [index]. price; gas = 0;} else {gas-= (station [next]. dis-station [index]. dis)/davg;} index = next;} else if (next! =-1) // at least the next more expensive site can be reached {if (station [index]. dis + cmax * davg> = dest) {break; // jump out of the while LOOP} cost = cost + (cmax-gas) * station [index]. price; // gas filled with oil = cmax-(station [next]. dis-station [index]. dis)/davg; index = next;} else {break;} if (station [index]. dis + cmax * davg> = dest) {cost = cost + (dest-station [index]. dis)/davg-gas) * station [index]. price; cout <setiosflags (ios: fixed) <setprecision (2) <cost ;} else {cout <"The maximum travel distance =" <setiosflags (ios: fixed) <setprecision (2) <station [index]. dis + cmax * davg;} return 0 ;}