【PAT 1033】 To Fill or Not to Fill 贪心算法&模拟

来源:互联网 发布:java 面试题2017 编辑:程序博客网 时间:2024/06/09 20:23

1033. To Fill or Not to Fill (25)

时间限制
10 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

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

题意

从S到D一路有N个加油站(油价各异),计算最省钱的加油策略。

分析:

贪心策略:假设现在自己处于A站,要考虑的是A站要不要加油,加多少油的问题。找到当前可达范围内(距离A站cmax*davg)下一个要加油的站B。

A站可达范围内, 分三种情况:

①没有加油站,------- 快到终点了,则加适量油到终点;或者 Impossible,则A站加满油到哪算哪;

②有更便宜的加油站 ------- 则找到第一家比A便宜的加油站B,加尽可能少的油(也可能油够直接开过去)到B站;

③只有价格更高的加油站,------则当下A站加满油,寻找相对最便宜的加油站B 开过去。

代码:

#include <iostream>#include <fstream>#include <iomanip>using namespace std;ifstream fin("in.txt");#define cin finstruct Gas{float price;float dis;}gas[500];const int INF = 0x7fffffff;float d;//目标距离int n;//加油站个数int cmp(const void* A,const void* B){Gas* aa = (Gas*)A;Gas* bb = (Gas*)B;return aa->dis > bb->dis;}float findMin(int i,float range,int &target){//寻找可达范围内,若更贵则选择最便宜的;若更便宜则选择最近的一个float maxDis = gas[i].dis + range;float minPrice = INF;for(int j=i+1;j<n;j++){if(gas[j].dis <= maxDis && gas[j].dis < d){//既在可达范围内,又在终点前if(gas[j].price <= minPrice){//更贵则选择最便宜的minPrice = gas[j].price;target = j;if(minPrice < gas[i].price)break;//若更便宜则选择最近的一个}}else{//超出范围break;}}return minPrice;}int main(){float c,avg;cin>>c>>d>>avg>>n;int i;for(i=0;i<n;i++){cin>>gas[i].price>>gas[i].dis;}qsort(gas,n,sizeof(Gas),cmp);if(n == 0 || gas[0].dis != 0){cout<<"The maximum travel distance = 0.00"<<endl;return 0;}float sum = 0.0;float left = 0.0;float maxPerDis = c*avg;int target;i=0;while(1){float minPrice = findMin(i,maxPerDis,target);if(minPrice == INF){//可达范围内没有加油站,if(gas[i].dis+maxPerDis >= d){//在当下加油站加适量油到终点sum += ((d-gas[i].dis)/avg - left)*gas[i].price;cout<< fixed << showpoint << setprecision(2)<<sum<<endl;}else{//Impossible -> 加满油 能到哪算哪cout<<"The maximum travel distance = "<<setiosflags(ios::fixed)<<setprecision(2)<<float(gas[i].dis+maxPerDis)<<endl;}break;}else if(minPrice <= gas[i].price){//可达范围内有更便宜 加油站 -> 加尽可能少的油,能到下一个便宜站即可float needAmount = (gas[target].dis-gas[i].dis)/avg;if(needAmount <= left){//目前油量够直接到达下个更便宜站,直接前往left = left - needAmount;}else{//不够直接到,则加一点恰好能到即可sum += (needAmount-left)*gas[i].price;left = 0;}i = target;}else{//可达范围只有价格更高的->加满油sum += (c-left)*gas[i].price;left = c - (gas[target].dis-gas[i].dis)/avg;i = target;}}system("PAUSE");return 0;}

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户2014年2月13日 星期四 22:25:44 HKT部分正确231033C++ (g++)0790GH

测试点

测试点结果用时(ms)内存(kB)得分/满分0答案正确078012/121答案正确07903/32答案正确07102/23答案正确07902/24答案错误07900/25答案正确07503/36答案正确07901/1

悲催的有一个Case始终通不过,没有找到问题所在,有发现问题的亲请不吝赐教。


0 0