1033. To fill or not to fill (25)-Greedy algorithm

Source: Internet
Author: User

The topics are as follows:

With highways available, driving a car from Hangzhou to any other city are easy. But since the tank capacity of a car was limited, we have to find gas stations on the the-on-the-the-the-time. Different give Different price. You is asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains the one test case. For each case, the first line contains 4 positive Numbers:cmax (<=), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas, the car can run; and N (<=), 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 B Etween This station and the Hangzhou, for I=1,... N. All the numbers in a line is 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, 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 dist Ance the car can run, accurate up to 2 decimal places.

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

This is an investigation of the problem of greedy algorithm, in order to achieve the least amount of money to reach the end or run the farthest distance of the purpose, we are difficult to grasp the whole problem, through the greedy algorithm, can be the whole problem into a local problem, only stand in the current angle analysis of the most greedy (optimal) choice, so as to get the best solution The difficulty of the greedy problem lies in the analysis of the problem and the classification of the situation, once the situation is insufficiently considered, it is possible to lose the full plate.

For this topic, we will discuss the following situation.

For the current site S, can reach the maximum range is the full amount of oil can reach the distance, set full of oil can forward the distance of Maxtogo, in the S to S+maxtogo range, in the following cases to consider:

Ⅰ There are gas stations within this range

① has a cheaper gas station than the current site, because only from the smallest local considerations, if there are more than the current cheaper, to reach the nearest rather than the cheapest (only need to find the first one cheaper than the site when the break can be).

② all more expensive than s (easy to mistake)

2.1 If you cannot reach the destination from S, choose the cheapest one, and fill it with oil from S to reach that site.

2.2 If from S can reach the end point directly, then refueling from S to reach the end point, directly to the end.

Ⅱ no gas station in this range

① if from S can reach the end point directly, then add to can reach the end point, direct arrival.

② If you can't reach the finish line from S, fill it up and run how far you can.


Concrete implementation for the use of structure to store site information, press into the vector in ascending order, from front to back processing each site, with cur to represent the current site, when Cur is the last site after the scope, the end loop, the specific code is as follows:

#include <iostream> #include <vector> #include <stdio.h> #include <iomanip> #include <    algorithm>using namespace std; #define INF 99999999struct gasstation{double price;    Double dis; Gasstation (double _p, double _d): Price (_p), dis (_d) {}};int compare (gasstation A, gasstation b) {return A.dis < B. Dis;}    int main () {int cons,gascnt;    Double cap;    Double dis;    Double Price;    Double Dist;    Vector<gasstation> stations;    CIN >> Cap >> dis >> cons >> gascnt;    Double Maxtogo = cap * CONS;        for (int i = 0; i < gascnt; i++) {scanf ("%lf%lf", &price,&dist);    Stations.push_back (Gasstation (price,dist));    } sort (Stations.begin (), stations.end (), compare);        if (Stations[0].dis > 0) {printf ("the maximum travel distance = 0.00\n");    return 0;    } int cur = 0;    Double now_cap = 0;    Double sumprice = 0;    int curend = Stations.size ();    Double stationprice = 0; DoUble Stationdis = 0;    int hasstation = 0;    int dest = 0;    If there are multiple starting stations, choose the cheapest one to refuel.    It turns out that there is no such thing as an actual trap in the title.    int minprice = Stations[0].price; for (int i = 0; i < stations.size (); i++) {if (Stations[i].dis = = 0) {if (Minprice > Stations[i].pric            E) {cur = i;    }}else break;        } while (cur < curend) {stationprice = Stations[cur].price;        Stationdis = Stations[cur].dis;        Dest =-1;        hasstation = 0; for (int i = cur + 1; i < stations.size (); i++) {//First determine if the current site has no reachable if (Stations[i].dis-stationdis) <                = Maxtogo) {//Find a reachable site and find the most recent and cheapest.                Hasstation = 1;                Find the cheapest two cases, the first is cheaper than the current site, to reach the nearest meet the conditions of such a site.                Or more expensive than the current site, then refueling to get to the cheapest one.                These two judgments are conflicting, because there is less than the time when the cheapest choice is not the cheapest but the most recent, expensive when looking for the cheapest//So first to determine whether it is cheaper than the current, no further find the cheapest in the expensive. if (Stationprice > Stations[i].price) {//Find cheaper, break find here to ensure that the nearest is found.                    Dest = i;                Break                }}else{//Both have no reachable sites.            Break                }} if (hasstation! = 1) {//There is no reachable site if ((Dis-stationdis) <= Maxtogo) {//can run to the end, then refuel until you can run to the end                Double need = Dis-stationdis;                if (Now_cap * cons >= need) {//oil enough to reach break;                    }else{//Oil not enough, add to can run to the end of double last = (Need-now_cap * cons);                    Sumprice + = (last/cons) * stationprice;                Break                }}else{//Can't run to the end, how far can run how far double Sumdis = Stationdis + cap * CONS;                printf ("The maximum travel distance =%.2lf\n", Sumdis);            return 0; }}else{//have a reachable site if (dest! =-1) {//Find a gas station that is cheaper than current and closest to the current, refuel to run there, then continue to consider double ne at that site                ed = Stations[dest].dis-stationdis;   if (need <= Now_cap * cons) {//oil enough to reach                 Now_cap-= need/cons;                    }else{//Oil not enough, padded sumprice + = (Need-now_cap * cons)/cons * Stationprice; Now_cap = 0;            Run past there is no oil.} cur = dest;                }else{//No cheap, choose the cheapest fuel to run past. !!!                First see whether can reach the end, can arrive directly to the end, must pay attention to this situation!!!                    if ((Dis-stationdis) <= Maxtogo) {double need = Dis-stationdis;                    if (Now_cap * Cons < need) {Sumprice + = (Need-now_cap * cons)/cons * Stationprice;                } break;                } int minprice = INF;                int mincur =-1;                        for (int i = cur + 1; i < stations.size (); i++) {if ((Stations[i].dis-stationdis) < Maxtogo) {                            if (Stations[i].price < minprice) {minprice = Stations[i].price;              Mincur = i;          }}else{break;                }} cur = mincur;                Sumprice + = (cap-now_cap) * stationprice;            Now_cap = Cap-(Stations[cur].dis-stationdis)/cons;    }}} printf ("%.2lf\n", Sumprice); return 0;}


1033. To fill or not to fill (25)-Greedy algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.