PAT (Advanced Level) 1033. To Fill or Not to Fill (25)最省加油方案,贪心算法

来源:互联网 发布:你有毒网络用语 编辑:程序博客网 时间:2024/05/16 00:30

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are 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 (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that 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 are 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 that 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 distance 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
贪心算法。
1、在本站加满油可达的最远距离范围内,寻找第一个比本站便宜或与本站相等的加油站,在本站把油加到到达这个站时刚好用完,若油够用则不加。
2、若在可达范围内的加油站都比本站贵,则在本站加满油,开到可达范围内最便宜的一个站。
3、若可达范围内无加油站,则在本站加满油,开到没油,输出距离。
终点的油价设为0,保证最便宜,若其在前面加油站的可达范围内,则车会在前面的加油站加刚好能到终点的油。
/*2015.7.23cyq*/#include <iostream>#include <vector>#include <algorithm>#include <fstream>using namespace std;//ifstream fin("case1.txt");//#define cin finstruct station{double price;double distance;};bool cmp(const station &a,const station &b){return a.distance<b.distance;}int main(){int Cmax,D,N;double Davg;cin>>Cmax>>D>>Davg>>N;vector<station> sta(N+1);for(int i=0;i<N;i++){cin>>sta[i].price>>sta[i].distance;}sta[N].distance=D;//终点sta[N].price=0;sort(sta.begin(),sta.end(),cmp);if(sta[0].distance!=0){//起点无加油站printf("The maximum travel distance = 0.00");return 0;}int cur=0;//当前加油站号int fullCanGo=Cmax*Davg;//加满油能开的距离double result=0;double curC=0;//当前油量while(cur!=N){int next=cur+1;//目标加油站先设为下一个int cheap=next;//找到可达范围内最近的比本站更便宜的加油站while(next<=N&&sta[cur].price<sta[next].price&&(sta[cur].distance+fullCanGo>=sta[next].distance)){if(sta[next].price<sta[cheap].price)cheap=next;//标记可达范围内最便宜的(不一定比本站便宜)next++;}if(sta[cur].distance+fullCanGo<sta[cheap].distance){//无可达车站,上面的while没有执行printf("The maximum travel distance = %.2lf",sta[cur].distance+fullCanGo);return 0;}//最近的比本站更便宜的加油站,油加到刚好够到这个加油站就行if(sta[cur].distance+fullCanGo>=sta[next].distance){double need=(sta[next].distance-sta[cur].distance)/Davg;//需要的油量if(need>curC){//油不够开result+=(need-curC)*sta[cur].price;curC=0;}else{curC=curC-need;}cur=next;}else{//若都比本站贵,则在本站加满油,并将后面最便宜的一个设为目标result+=(Cmax-curC)*sta[cur].price;curC=Cmax-(sta[cheap].distance-sta[cur].distance)/Davg;cur=cheap;}}printf("%.2f",result);return 0;}


0 0