您的位置:首页 > 其它

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

2014-05-15 11:01 176 查看
题目

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


贪心,循环情况分类:

1、如果加满油后也无法到达最近的站,则结束。

2、如果有可以到达的站,分为2种情况:

2.1、如果可以到达的站中有比当前站便宜的,去第一个比当前站便宜的站,并在此站加刚好可以到那个站的油(因为那站的油比之前的都便宜,可以减小后面那段路的代价)。刷新位置到那个站,清空油箱,刷新价格,重新循环

2.2、如果没有,去其中最便宜的一个站,并在当前站加满油(因为这是这个满油箱路程中最便宜的油)。刷新位置到那个站,刷新油箱,刷新价格,重新循环。

代码:

#include <iostream>
#include <algorithm>
using namespace std;

struct way	//站的信息
{
double dis;	//距离
double price;	//价格
};

bool Cm_dis(const way &s1,const way &s2);	//排序站

int main()
{
double cmax,d,davg;	//基本信息
int n;
cin>>cmax>>d>>davg>>n;

double tank=0.0;	//油箱油量,初始化为0
double money=0.0;	//花费,初始化为0
way station[501];	//站
int pos=0;	//当前位置,初始化为0(起点)
const double dmax=cmax*davg;	//满油车程

int i;

for(i=0;i<n;i++)	//输入信息(这里需要保证没有站在终点及终点以后)
cin>>station[i].price>>station[i].dis;
station
.dis=d;		//!!!在最后增加一个虚拟的终点站
station
.price=0.00;	//!!!并把油价设为0,保证使得可以在之后的判断中到达终点站
sort(station,station+i,Cm_dis);	//按距离排序

int max_s;	//加满油能到达的最远站
int cheap_s;
//!!!加满油能到达站中最便宜的加油点(如果这个站比当前的贵),第一个比当前站便宜的站(如果存在)
cout<<fixed;
cout.precision(2);

if(station[0].dis!=0)	//!!!油箱为空,如果起始站没有油加结束
cout<<"The maximum travel distance = 0.00";
else
{
while(pos<n)	//没有到达终点
{
max_s=pos;
cheap_s=pos+1;
while(max_s<n&&station[max_s+1].dis-station[pos].dis<=dmax)	//没有到达终点,且可以到达下一站,刷新可以到达的最远站
{
max_s++;
if(station[cheap_s].price>station[pos].price	//最便宜的可达站价格贵于当前站
&&station[max_s].price<station[cheap_s].price)	//且下一站的价格比当前的便宜
cheap_s=max_s;
}

if(max_s==pos)	//不能到达下一站
{
cout<<"The maximum travel distance = "<<station[pos].dis+dmax;
return 0;
}
else if(station[cheap_s].price<station[pos].price)	//有比当前站便宜的可达站,加刚好的油,去那里
{
money+=((station[cheap_s].dis-station[pos].dis)/davg-tank)*station[pos].price;
tank=0;
pos=cheap_s;
}
else	//没有,加满油,去可达站中最便宜的那个站
{
money+=(cmax-tank)*station[pos].price;
tank=cmax-(station[cheap_s].dis-station[pos].dis)/davg;
pos=cheap_s;
}
}
cout<<money;
}

return 0;
}

bool Cm_dis(const way &s1,const way &s2)
{
return s1.dis<s2.dis;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: