您的位置:首页 > 其它

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

2013-08-29 15:58 435 查看
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 far
if(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 far
if(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
;
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);
else
printf("The maximum travel distance = %.2f\n",cost);
return 0;

}


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

50 1000 10 6
7.50 0
7.00 900
7.30 200
7.20 300
7.40 100
7.10 400

50 1000 10 8
8.00 1000
7.50 0
7.50 950
7.40 100
7.20 300
7.00 900
7.10 400
7.30 200

719.00
50 1300 10 2
7.10 10
7.00 400
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: