1033. To-fill or not-to-fill

Source: Internet
Author: User

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 one test case. For each case, the first line contains 4 positive numbers:cmax  (<=), the maximum C Apacity 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 (<= 500), 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 between this station and 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.


Tangled up with me all the afternoon and finally it's all kinds of Gaga to figure out

#include <stdio.h> #include <stdlib.h> #include <vector> #include <algorithm>using namespace        Std;typedef struct{float Price;        int dis,period;    }gas;vector<gas> gas;float sum;bool cmp (gas a,gas b); int main () {int cop,distance,per,num,i;    scanf ("%d%d%d%d", &cop,&distance,&per,&num);    Gas temp;         int far=cop*per;//How far up to run for (i=0;i<num;i++) {scanf ("%f%d", &temp.price,&temp.dis);         Gas.push_back (temp);    } sort (Gas.begin (), Gas.end (), CMP);    sum=0.0f;        if (gas[0].dis!=0) {printf ("the maximum travel distance =%.2f\n", sum);        return 0;         } for (i=0;i<num-1;i++) {Gas[i].period=gas[i+1].dis-gas[i].dis;             if (Gas[i].period>far) {Sum+=far;             printf ("The maximum travel distance =%.2f\n", sum);             return 0;         } sum+=gas[i].period; } gas[i].period=distance-gas[i].diS             if (Gas[i].period>far) {Sum+=far;             printf ("The maximum travel distance =%.2f\n", sum);             return 0;    }//Sum+=gas[i].period;    Float cost,min=gas[0].price;//the smallest price int now,left;    Now=gas[0].period;    sum=now;//currently walking the total distance left=far-gas[0].period;//can also walk the distance cost=now*gas[0].price;//at this station spend for (i=1;i<num;i++) {if (gas[i].price<=min)//At this station refueling {min=gas[i].price;//Update minimum price now=gas[i].pe             Riod             Left=far-now;             Cost+=now*gas[i].price; } else {if (gas[i].period<=left)//continue before the refueling {Now+=gas[i].perio                 D                 Left=far-now;                 Cost+=gas[i].period*min;                 } else//start a new refueling {cost+=left*min;                 Min=gas[i].price;                 Now= (Gas[i].period-left);         Cost+=gas[i].price*now;        Left=left-now;    }}} printf ("%.2f\n", cost/per);    System ("pause");    return 0;     }bool cmp (gas A,gas b) {return a.dis<b.dis; }



1033. To-fill or not-to-fill

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.