1033. To Fill or Not to Fill (25)-PAT

来源:互联网 发布:淘宝上的折扇哪家好 编辑:程序博客网 时间:2024/05/24 06:42
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 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:

The maximum travel distance = 1200.00

推荐指数:※※

来源:http://pat.zju.edu.cn/contests/pat-a-practise/1033

贪心,关键是贪心的策略。

想象一下自己在高速公路开车要加油的场景。

1.搜索当前可以到达的最便宜加油站的价格,与当前加油站价格比较。

如果比当前加油站大,那在当前加油站加满。到当前可以到达的最便宜的加油站的时候,再看要不要补充油。

如果比当前加油站小,这加正好可以到那个加油站的油。

重复,直到到达终点。

#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<float.h>using namespace std;typedef struct Station{float price;int dis;}Station;int compare(const void *a, const void *b){return ((Station*)a)->dis-((Station*)b)->dis;}float cheapest_price(float dis,int avg,int cap,Station *s,int n,bool *is_reach){if(s[0].dis>0){*is_reach=false;return 0;}int i,j;float curr_dis=0;float total_cost=0;float max_can=avg*cap;i=0;while(i<n){float tmp_cheap=FLT_MAX;int chs=-1;for(j=n-1;j>i;j--){//in the can reach station , choose a cheap and farif(s[j].dis<=s[i].dis+max_can&&s[j].price<tmp_cheap){tmp_cheap=s[j].price;chs=j;}}if(-1==chs){if(s[i].dis+max_can>=dis){total_cost+=((dis-curr_dis)/avg)*s[i].price;*is_reach=true;return total_cost;//if can reach ,return cost}else{*is_reach=false;curr_dis=s[i].dis+max_can;return curr_dis;//if can't reach, return dis}}else{if(tmp_cheap>s[i].price){if(s[i].dis+max_can>=dis){total_cost+=((dis-curr_dis)/avg)*s[i].price;*is_reach=true;return total_cost;//if can reach ,return cost} else{total_cost+=(cap-(curr_dis-s[i].dis)/avg)*s[i].price;curr_dis=s[i].dis+max_can;}}else{chs=-1;for(j=i+1;j<n;j++){//in the can reach station , choose a cheap and farif(s[j].price<=s[i].price){chs=j;break;}}total_cost+=((s[chs].dis-curr_dis)/avg)*s[i].price;curr_dis=s[chs].dis;}i=chs;}}}int main(){int cap,dis,avg,n;int i;scanf("%d%d%d%d",&cap,&dis,&avg,&n);Station *s=new Station[n];for(i=0;i<n;i++)scanf("%f%d",&s[i].price,&s[i].dis);qsort(s,n,sizeof(Station),compare);bool is_reach=false;float cost=cheapest_price(dis,avg,cap,s,n,&is_reach);if(true==is_reach)printf("%.2f\n",cost);elseprintf("The maximum travel distance = %.2f\n",cost);return 0;}

给几组测试用例,感谢@badmartin提供:

50 1000 10 67.50 07.00 9007.30 2007.20 3007.40 1007.10 400

50 1000 10 88.00 10007.50 07.50 9507.40 1007.20 3007.00 9007.10 4007.30 200719.00
50 1300 10 27.10 107.00 400



原创粉丝点击