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

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 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

题意

从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 fin


struct 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 部分正确 23 1033 C++ (g++) 0 790 GH

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 780 12/12
1 答案正确 0 790 3/3
2 答案正确 0 710 2/2
3 答案正确 0 790 2/2
4 答案错误 0 790 0/2
5 答案正确 0 750 3/3
6 答案正确 0 790 1/1

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

    原文作者:贪心算法
    原文地址: https://blog.csdn.net/tiantangrenjian/article/details/19175831
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞