pat1033. To-fill or not-to-fill (25)

Source: Internet
Author: User

1033. To-fill or not-to-fill (25) time limit of ten MS memory limit 65536 KB code length limit 16000 B procedure StandardAuthor Zhang, Guochuan

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

Submit Code

1#include <cstdio>2#include <stack>3#include <algorithm>4#include <iostream>5#include <stack>6#include <Set>7#include <map>8 using namespacestd;9 struct StationTen { One     DoublePrice,dis; A }; -Station sta[505]; - BOOLCMP (station a,station b) the { -     if(a.dis==B.dis) -     { -         returna.price<B.price; +     } -     returna.dis<B.dis; + } A //find the first gas station at the nearest petrol station with a lower oil price than the current one, at the current petrol station, just fill the oil at //if not found, see if the current can reach the destination, if possible, as long as the oil added to the destination; - //if not, find the lowest oil gas station in the range, fill the current gas station with oil, and go to the next gas station - //If a gas station is not found in the range, the destination cannot be reached, the maximum distance from the output = The location of the current gas station + the distance of the full oil - //In addition to the label, the others are double values!!  - intMain () - { in     //freopen ("D:\\input.txt", "R", stdin); -     intN; to     DoubleD,davg,cmax; +scanf"%lf%lf%lf%d",&cmax,&d,&davg,&N); -     inti; the      for(i=0; i<n; i++) *     { $scanf"%LF%LF",&sta[i].price,&Sta[i].dis);Panax Notoginseng     } -sta[i].dis=D; thesta[i].price=-1; +Sort (sta,sta+n+1, CMP); A     Doublefardis=cmax*Davg; the     if(sta[0].dis!=0)//The first gas station is not at the destination +     { -printf"The maximum travel distance = 0.00\n"); $         return 0; $     } -     if(d==0)//The first gas station is a destination . -     { theprintf"0.00\n"); -         return 0;Wuyi     } the     intCursta=0, Nextsta; -     Doubletank=0; Wu     Doubletotalmon=0; -      while(sta[cursta].price!=-1)//not to the destination About     { $         DoubleMinprice=Sta[cursta].price; -         intminsta=-1; -          for(nextsta=cursta+1; nextsta<=n&&sta[cursta].dis+fardis>=sta[nextsta].dis; nextsta++) -             //find the cheapest point for the nearest oil charge A         { +             if(sta[nextsta].price<=minprice) the             { -Minsta=Nextsta; $                  Break; the             } the}//just get it . the         if(minsta!=-1)//There could be a destination, maybe a gas station. the         { -             Doublet= (Sta[minsta].dis-sta[cursta].dis)/Davg; in             //cout<<t<<endl; the             if(tank<t) the             { Abouttotalmon+=sta[cursta].price* (ttank); thetank=0; the             } the             Else +             { -tank-=T; the             }BayiCursta=Minsta; the         } the         Else -{//The destination is not within the current reach and can range from petrol stations with higher oil prices or gas stations -              for(nextsta=cursta+1; nextsta<=n&&sta[cursta].dis+fardis>=sta[nextsta].dis; nextsta++) the             { the                 if(sta[nextsta].price>minprice) the                 { theMinprice=Sta[nextsta].price; -Minsta=Nextsta; the                      Break; the                 } the             }94              for(nextsta++; nextsta<=n&&sta[cursta].dis+fardis>=sta[nextsta].dis; nextsta++) the             { the                 if(sta[nextsta].price>sta[cursta].price&&sta[nextsta].price<minprice) the                 {98Minprice=Sta[nextsta].price; AboutMinsta=Nextsta; -                 }101             }102             if(minsta!=-1)//There are gas stations103             {104totalmon+= (Cmax-tank) *Sta[cursta].price; thetank=cmax-(Sta[minsta].dis-sta[cursta].dis)/Davg;106Cursta=Minsta;107             }108             Else //not reached109             { theprintf"The maximum travel distance =%.2lf\n", sta[cursta].dis+Fardis);111                 return 0; the             }113         } the     } theprintf"%.2lf\n", Totalmon); the     return 0;117}

pat1033. To-fill or not-to-fill (25)

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.