1033 To Fill or Not to Fill (25 point(s))

1033 To Fill or Not to Fill (25 point(s))

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: C​max​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then Nlines follow, each contains a pair of non-negative numbers: P​i​​, the unit gas price, and D​i​​ (≤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 Xis 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

参考链接:

https://blog.csdn.net/xyt8023y/article/details/46425435

https://blog.csdn.net/joyceyang_999/article/details/82730818

复杂模拟题。难!

到达加油站,在当前的最大可达范围内:

 A.有其它加油站

1.如果有加油站比该站的价格低,那么加油使到达最近的站; 

2.如果有加油站但是价格更贵

a)不能到达终点,则加满,然后到可达范围内最便宜的加油站再判断(加满是因为,虽然在范围之外更低的油价,但是即使加满也无法到达,还需要中间一个加油站中转,显然这个中转加油站的油价更高,所以既然当前油价最低,能加满就加满,之后更高油价的油就可以少加点)

b)可以到达终点,在当前位置加满所需的油(或者不再加油),刚好到达终点 

B.没有其它的加油站

1.可以到达终点,在当前位置加满所需的油(或者不再加油),直接到达

2.不能到达终点,加满油后能跑多远跑多远

#include<iostream>
#include<algorithm>
#include<vector>
#define INF 0x3f3f3f3f
using namespace std;
struct station{
	double price,dis;//油价,离杭州距离,到下一个油站距离 
	bool operator < (const station &other) const{
		return dis<other.dis;
	}
};
vector<station> s;
int main(void){
	double c_max,D,d_avg;int N;//油箱容量,距离杭州的距离,公里/L,油站数量
	cin>>c_max>>D>>d_avg>>N;
	station sta;
	for(int i=0;i<N;i++){
		cin>>sta.price>>sta.dis;
		s.push_back(sta);
	}
	sort(s.begin(),s.end());
	/*到达加油站,在当前的最大可达范围内:
	A.有其它加油站
	1.如果有加油站比该站的价格低,那么加油使到达最近的站; 
	2.如果有加油站但是价格更贵
	a)不能到达终点,则加满(加满是因为,无论在范围之外有多小的油价,即使加满也无法到达,还需要中间一个加油站中转,但既然当前油价最低,能加满就加满,之后油钱高的就可以少加点)
	b)可以到达终点,加油使得刚好到达终点 
	B.没有其它的加油站
	1.可以到达终点,直接到达
	2.不能到达终点,加满油后能跑多远跑多远 
	*/
	if(s[0].dis>0){
		cout<<"The maximum travel distance = 0.00"<<endl;
		return 0; 
	} 
	double sumDis,sumPrice=0;//当前距离,总价格 
	double curCap=0;//当前油量
	int cur=0;//当前所在的油站 
	double maxToGo = c_max*d_avg;//最大可达范围 
	while(cur<s.size()){ 
		bool hasStation= false; 
		double stationPrice = s[cur].price;
		double stationDis = s[cur].dis;
		int dest=-1;
		
		for(int i=cur+1;i<s.size();i++){
			if(s[i].dis<=stationDis+maxToGo){
				hasStation = true;
				if(stationPrice>s[i].price){
					dest = i;break;//记录最近的低油价站 
				}
			}
			else break;
		}
		if(hasStation == false){//当前可达范围内没有油站了 
			if(stationDis+maxToGo>=D){//如果可以到达终点 
				double restDis = D-stationDis;//剩余的距离 
				if(curCap*d_avg>=restDis){//如果不再需要额外的油
					break;
				}else{
					double needDis = restDis - curCap * d_avg;//需要加油到达的路程 
					sumPrice+=(needDis / d_avg) *stationPrice; 
					break; 
				} 
			}else{//如果不能到达终点 
				sumDis = stationDis+maxToGo;
				sumPrice += (D-curCap)*stationPrice;
				printf("The maximum travel distance = %.2lf\n",sumDis);
                return 0;
			}
		}
		else{//当前可达范围内有油站
			if(dest!=-1){//在可达范围内有比当前油站更便宜的油站 
				double gapDis = s[dest].dis - stationDis;//到最近的油站的距离 
				if(curCap*d_avg >= gapDis) curCap-= gapDis/d_avg;//当前的油量足够
				else{//需要额外的加油,加油使得能够刚好到这个站 
					sumPrice +=(gapDis-curCap*d_avg)/ d_avg*stationPrice;
					curCap = 0;//因为是刚好的,跑过去就没有油了 
				} 
				cur=dest; 
			} 
			else{//在可达范围内没有更便宜的油
				if(stationDis+maxToGo>=D){//在可达范围内可以到达终点,直接到达 
				 	double restDis = D-stationDis;//如果可以通过加油直接到达 
				 	if(curCap*d_avg < restDis){//还需要额外的油 
				 		sumPrice+=(restDis-curCap*d_avg)/d_avg*stationPrice;
				 		break;
					} 
					else break;//不需要额外的油了,直接结束 
				} 
				//在可达范围内不可以到达终点,没有更便宜的油,加满,然后到达可达范围内最便宜的下一个油站 
				int minPrice = INF;
				int minCur = -1;
				for(int i=cur+1;i<s.size();i++){
					if(s[i].dis < stationDis + maxToGo){
						if(s[i].price < minPrice){
							minPrice = s[i].price;
							minCur = i;
						} 
					} 
					else break;
				} 
				cur = minCur;// 到达可达范围内最便宜的下一个油站  
				sumPrice +=(c_max-curCap)*stationPrice;//加上达到满油的价格 
				curCap = c_max-(s[minCur].dis-stationDis)/d_avg;	//调整达到下一个油站的油量 
			}
		} 
	}
	printf("%.2lf\n",sumPrice);
	return 0; 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值