您的位置:首页 > 其它

1033. To Fill or Not to Fill (25)

2015-01-30 22:18 411 查看
题目:

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、贪心策略具体为:
假设我们现在在A点,A能到达的最远距离之内有B、C、D三个点
1)如果B、C、D中有油价比A小的,则找到离A最近的,在A处加的油刚好到该处
2)如果B、C、D中没有油价比A小的,则找到这里面油价最小的,在A处加满
3、当然也需要注意一些边值条件,譬如什么时候结束计算、相邻两个站点之间的距离大于满油能跑的距离、起始点没有加油站等等。

代码:

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

struct station
{
double len;//the distance between this station and Hangzhou
double price;//the unit gas price
bool operator < (const station &s) const
{
return len<s.len;
}
};

//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
//N (<= 500): the total number of gas stations
int Cmax,D,Davg,N;
vector<station> gasStation;
double cost=0;
//贪心策略:
//假设我们现在在A点,A能到达的最远距离之内有B、C、D三个点
//1、如果B、C、D中有油价比A小的,则找到离A最近的,在A处加的油刚好到该处
//2、如果B、C、D中没有油价比A小的,则找到这里面油价最小的,在A处加满
void mincost(int curStation,double leftgas)
{
if((curStation<N-1 && gasStation[curStation+1].len-gasStation[curStation].len>Cmax*Davg)
|| (curStation==N-1 && D-gasStation[curStation].len>Cmax*Davg))
{ //当前站到下一站的距离大于汽车满油能跑的距离,故无法到达终点
cout<< "The maximum travel distance = "<<fixed<<setprecision(2)<<gasStation[curStation].len+Cmax*Davg<<endl;
return;
}
double minPrice=65535;
int i,index=curStation,flag=0;
for(i=curStation+1;i<N && gasStation[i].len-gasStation[curStation].len<=Cmax*Davg;++i)
{
if(gasStation[i].price<minPrice)
{ //寻找最大里程范围内最便宜的,如果有比当前站更便宜的直接跳出
minPrice=gasStation[i].price;
index=i;
if(minPrice<gasStation[curStation].price)
{flag=1; break;}
}
}
if(flag)
{ //如果B、C、D中有油价比A小的,则找到离A最近的,在A处加的油刚好到该处
cost += gasStation[curStation].price*((gasStation[index].len-gasStation[curStation].len)/Davg-leftgas);
mincost(index,0);
}
else
{ //如果B、C、D中没有油价比A小的,则找到这里面油价最小的,在A处加油
if(D-gasStation[curStation].len<=Cmax*Davg)
{ //如果此站可到达终点,则加上刚好到达终点的油,输出价格
cost += gasStation[curStation].price*((D-gasStation[curStation].len)/Davg-leftgas);
cout<<fixed<<setprecision(2)<<cost;
return;
}
else
{
cost += gasStation[curStation].price*(Cmax-leftgas);
mincost(index,Cmax-(gasStation[index].len-gasStation[curStation].len)/Davg);
}
}
}

int main()
{
cin>>Cmax>>D>>Davg>>N;
for(int i=0;i<N;++i)
{
station s;
cin>>s.price>>s.len;
gasStation.push_back(s);
}
sort(gasStation.begin(),gasStation.end());
int dis=0;//the current distance
int gas=0;//the current gas left
if(gasStation[0].len>0)
cout<< "The maximum travel distance = 0.00" <<endl;
else
mincost(0,0);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: