可恶心了,先找出最低的加油站,如果最低的站就是当前站,则看看能不能走到终点,否则加满,然后判断一下能不能走到下一站,能走就走下一站,不能走能走多少是多少。如果最优站不是本站,然后看看不加油能不能走到最优站,如果不能,就找与之中间的次优站,加足以走到次优站的油。。。每走一站就dfs一下。
题目描述
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.
输入描述:
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.
输出描述:
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.
输入例子:
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
输出例子:
749.17
#include<bits/stdc++.h>
#include <iostream>
#include <map>
#include <string>
#include<vector>
#include<stack>
using namespace std;
struct Station{
int dis;
double price;
}s[100005];
bool cmp(Station a,Station b)
{
return a.dis<b.dis;
}
double cmax,avg;
int d,n;
int dis=0;
double dfs(int idx,double tanknow)
{
static double maxdistance=cmax*avg;//最大距离
static double count = 0;
if(dis+tanknow*avg>=d)
{
dis=d;
return count;
}
double nowp=s[idx].price;
double minp=nowp;
int pos=idx,p=idx+1;
while(p<n)
{
if(maxdistance>s[p].dis-dis)
{
if(s[p].price<minp)
{
pos=p;
minp=s[p].price;
}
}
else break;
p++;
}//找出最低价格加油站
if(minp==nowp)//当前站是最低价格
{
if(maxdistance+dis>d)//不用加油可以走到
{
int len=d-dis;
double c=len/avg;
dis=d;
return count+(c-tanknow)*minp;
}
count+=(cmax-tanknow)*minp;//加满
if(idx+1>=n||maxdistance+dis<s[idx+1].dis)//无法到达下一个加油站
{
dis+=maxdistance;
return count;
}
int len=s[idx+1].dis-dis;
double c=len/avg;
dis=s[idx+1].dis;
return dfs(idx+1,cmax -c);//走一站
}
//
int len=s[pos].dis-dis;
double c=len/avg;
if(c<=tanknow)
{
dis=s[pos].dis;
return dfs(pos,tanknow-c);
}//走一站
//走不到最低价格站,找与之中间最低价格
for(int i=idx+1;i<pos;i++){
if(s[i].price<nowp)
{
int len=s[i].dis-dis;
double c=len/avg;
dis=s[i].dis;
if(c<=tanknow)return dfs(i,tanknow-c);//不用加油
count+=(c-tanknow)*nowp;
return dfs(i,0);//足以走到下一站
}
}
//找不到
dis=s[pos].dis;
count+=(c-tanknow)*nowp;//加满
return dfs(pos,0);
}
int main() {
cin>>cmax>>d>>avg>>n;
for(int i=0;i<n;i++)
cin>>s[i].price>>s[i].dis;
sort(s,s+n,cmp);
while(s[n-1].dis>=d)n--;
if(s[0].dis!=0)
{
cout << "The maximum travel distance = 0.00";
return 0;
}
double count=dfs(0,0);
if (dis - d)printf("The maximum travel distance = %.2f", (float)dis);
else printf("%.2f", count);
return 0;
}