Topic 1437:to Fill or not to Fill (greedy algorithm)

Source: Internet
Author: User

Title Description:

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:

for, the first line contains 4 positive Numbers:cmax (<=), the maximum capacity of th e 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:

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:
50 1300 12 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 30050 1300 12 27.10 07.00 600
Sample output:
749.17The Maximum Travel distance = 1200.00

Analysis:

greedy strategy: Suppose now oneself in a station, to consider is a station to not refueling, add how much oil problem. Locate the current range ( distance from a station CMAX*DAVG) The next station to refuel B.

A station can reach within three kinds of situations:

① There is no gas station,-------to the end, then add the right amount of oil to the end, or impossible, then a station full of oil to what count;

② have cheaper gas stations-------find the first gas station B cheaper than a, add as little oil as possible (or the oil can go straight over) to station B;

③ only a higher-priced petrol station,------a station full of oil, looking for the relatively cheapest gas station B to drive past.

Make a greedy choice for every gas station.

Code:

#include <iostream> #include <stdio.h> #include <algorithm> #include <math.h> #include < Stdlib.h> using namespace std;    struct gas_station{float price;     int distance;    BOOL operator < (const gas_station & A) const {return distance < a.distance;    }}buff[501];int Main () {int cmax,d,davg,n;    int dis; while (scanf ("%d%d%d", &cmax,&d,&davg,&n)!=eof) {for (int i = 0; i < N; i++) {scanf        ("%f%d", &buff[i].price,&buff[i].distance);        } buff[n].distance = D;         Buff[n].price = 100000000;         Sort (buff,buff+n);            if (Buff[0].distance > 0) {printf ("the maximum travel distance = 0.00\n");        Continue dis = Cmax * davg;//maximum single drive distance float sum = 0;//Total cost float Temp,remind_gas = 0;//remaining petrol amount int I, k        ;            for (i = 0; i < n;i++) {k = i+1; if (i! = 0) {Remind_gas-= (float) (buff[i].distance-buff[i-1].distance)/davg;                }//Find the current station after the first price lower than it gas station for (; k < N && buff[i].price <= buff[k].price;k++)            Continue                if (buff[k].distance-buff[i].distance > Dis) {sum + = (cmax-remind_gas) *buff[i].price;            Remind_gas = Cmax;                }else{temp = (float) (buff[k].distance-buff[i].distance)/davg-remind_gas;                    if (fabs (temp) >1e-5&&temp >0) {sum + = Temp*buff[i].price;                Remind_gas = (float) (buff[k].distance-buff[i].distance)/davg; }} if (Buff[i+1].distance-buff[i].distance>dis) {Double total_d = (double) (buf                F[i].distance + dis);                printf ("The maximum travel distance =%.2f\n", total_d);            Break        }} if (I==n) {printf ("%.2f\n", sum);  }} return 0;}


Attention:

Compare floating-point numbers greater than 0 and need to add fabs (temp), because this place also WA once.







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Topic 1437:to Fill or not to Fill (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.