To-fill or not-to-fill (greedy simulation)

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.

1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <cstring>5 using namespacestd;6  7typedefstruct{8     Doublepri;9     intdis;Ten }station; One   A BOOLcmpConstStation &AMP;A,ConstStation &B) { -     if(a.dis!=B.dis) -         returna.dis<B.dis; the     Else -         returna.pri<B.pri; - } - intCmax, D, Davg, TMP1; + intN, I, J, Tmp_ind; - Doubletank, Now_pos, Now_pri, Tot_cost, comp, tmp_pos; + BOOLflag, Flag2, Flag3, Flag4; AStation s[550]; at intMain () { -      while(~SCANF ("%d%d%d%d",&cmax,&d,&davg,&N)) { -memset (S),0,sizeof(S)); -tank=0; -now_pos=0; -tot_cost=0; in          for(i=0; i<n;i++) -scanf"%lf%d",&s[i].pri,&S[i].dis); toSort (s,s+n,cmp); +s[n].dis=D; -S[n].pri=0; the         if(s[0].dis!=0|| n==0){ *printf"The maximum travel distance = 0.00\n"); $             Continue;Panax Notoginseng         } -          for(i=0; i<n;i++){ theflag=false; +Flag2=false; Aflag3=false; theflag4=false; +             if(i+1<N) { -comp=s[i+1].pri; $tmp_pos=s[i+1].dis; $             } -              for(j=i+1; j<=n;j++){ -                 if(s[j].dis<=now_pos+cmax*davg&&s[j].dis>=Now_pos) { theflag=true; -                     if(s[j].pri<S[i].pri) {Wuyi                         if(s[j].dis<=now_pos+tank*Davg) { the                             //The price is lower, the existing oil can be opened there -tank-= (S[j].dis-now_pos)/Davg; Wunow_pos=S[j].dis; -                         } About                         Else{ $                             //The price is low, go there and refuel -Tot_cost+=s[i].pri* ((S[j].dis-now_pos)/davg-tank); -tank=0; -now_pos=S[j].dis; A                         } +i=j-1; theFlag2=true; -                          Break; $                     } the                     Else{ theflag4=true; the                         if(s[j].pri<=comp) { thecomp=S[j].pri; -tmp_pos=S[j].dis; inTmp_ind=J; the                         } the                     } About                 } the             } the             if(FLAG2) the                 Continue; +             if(now_pos+cmax*davg>=Tmp_pos) { -                 //at the petrol station, the price is relatively low, fill the local oil, and drive to the gas station. thetot_cost+= (Double) (Cmax-tank) *S[i].pri;BayiTank= (Double) (Cmax-(Double) (Tmp_pos-now_pos)/Davg); thenow_pos=Tmp_pos; the             } -             Else{ -                 //can't get to the gas station . thenow_pos+=cmax*Davg; thetot_cost+= (Double) (Cmax-tank) *S[i].pri; theprintf"The maximum travel distance =%.2lf\n", Now_pos); theflag3=true; -                  Break; the             } the             if(FLAG4) thei=tmp_ind-1;94             if(!flag&&now_pos< (Double) (D) { theprintf"The maximum travel distance =%.2lf\n", now_pos+tank*Davg); theflag3=true; the                  Break;98             } About         } -         if(!flag3)101printf"%.2lf\n", tot_cost);102     }103 }104   the /**************************************************************106 problem:1437107 user:blueprintf108 language:c++109 result:accepted the time:10 Ms111 memory:1532 KB the ****************************************************************/
View Code

It feels like I'm doing it with a priority queue, but it's a little different from the problem.

The original question is POJ 2431

Test instructions is there are N gas station, to walk L unit distance, ask the truck can reach the end, if possible, the least number of refueling?

When the fuel tank is out of oil, from the big to the small to take out the gas station has been passing the most fuel gas stations, using priority queue for

The test instructions is that there are N gas stations, but the tank has a cap,

Ask if you can reach the end, if not reach, output the farthest distance, if it can reach, the output of the minimum cost.

is actually a greedy question,

If there is a petrol station in the area where full oil is available, it is cheaper than the petrol station here, and the least amount of oil arrives at the first cheap petrol station;

Otherwise, fill the ground with oil and then go to the cheapest gas station to make the next choice.

There is a bit of a problem when you hit the code and you need to decide when you can't reach the end.

To-fill or not-to-fill (greedy simulation)

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.