1033. To Fill or Not to Fill (25)
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
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#define inf 1000000
using namespace std;
struct STATION
{
double price;
int dis;
bool operator<(const STATION& s)const
{
return dis < s.dis;
}
};
int main()
{
int N;
double cap, distan, speed;
cin >> cap >> distan >> speed >> N;
vector<STATION>highway(N+1);
int i, j;
for (i = 0; i < N; i++)
cin >> highway[i].price >> highway[i].dis;
highway[N].dis = distan;
highway[N].price = 0;
sort(highway.begin(), highway.end());
float nowcost=0,nowtank=0;
float maxdis = 0,nowdis=0,minpri=inf,able=0;
int next,last=0;
i = 0;
if (highway[0].dis != 0)printf("The maximum travel distance =0.00");
while (i != N)
{
nowtank -= (highway[i].dis - highway[last].dis) / speed;
able =highway[i].dis+ nowtank * speed;
minpri = inf; next = -1;
for (j = i + 1; j <= N && highway[j].dis < able; j++)
{
if (highway[j].price < minpri)
{
minpri = highway[j].price;
next = j;
}
}
if (minpri < highway[i].price);
else
{
able = highway[i].dis + cap * speed;
if (able < highway[i+1].dis)
{
maxdis = able;
break;
}
next = -1;
for (j = i + 1; j <= N && highway[j].dis < able; j++)
{
if (highway[j].price < highway[i].price)
{
next = j;
break;
}
}
if (next!=-1)
{
nowcost += ((highway[next].dis - highway[i].dis) / speed - nowtank)*highway[i].price;
nowtank = (highway[next].dis - highway[i].dis) / speed;
}
else
{
nowcost += (cap - nowtank)*highway[i].price;
nowtank = cap;
next = i + 1;//或许应该跳到次优点;
}
}
//
last = i;
i = next;
}
if (maxdis != 0)printf("The maximum travel distance = %.2f", maxdis);
else printf("%.2f", nowcost);
return 0;
}
再贴一个能过的。
#include <stdio.h>
#include <algorithm>
using namespace std;
struct Station
{
double p;
double d;
bool operator <(const Station &a) const
{
return d<a.d;
}
}buf[501];
struct Tank
{
double left;
double cost;
double d;
}tank;
int main()
{
double Cmax,D,Davg;
int n;
while (scanf("%lf %lf %lf %d", &Cmax, &D, &Davg, &n)!=EOF)
{
for (int i=0;i<n;++i)
{
scanf("%lf %lf", &buf[i].p, &buf[i].d);
}
buf[n].d=D;
buf[n].p=0;
double farest=Cmax*Davg;
sort(buf, buf+n);
int i=0;
tank.left=0;
tank.cost=0;
tank.d=0;
if (buf[0].d!=0)
printf("The maximum travel distance = 0.00\n");
else
{
while (i<n)
{
double lowest=buf[i].p;
int index=i;
for (int j=i+1;j<=n&&buf[j].d-buf[i].d<=tank.left*Davg;++j) //找出油耗完前比当前站便宜的最低价站
if (buf[j].p<lowest)
{
index=j;
lowest=buf[j].p;
}
if (index!=i) //对应上述状态1,直接开过去加油
{
tank.left-=(buf[index].d-buf[i].d)/Davg;
i=index;
tank.d=buf[i].d;
}
else //否则,找出满油箱可到达范围内最近的比当前站低价站
{
for (int j=i+1;j<=n&&buf[j].d<=buf[i].d+farest;++j)
{
if (buf[j].p<lowest)
{
index=j;
break;
}
}
if (index!=i) //对应上述状态2,加上刚好够的油开过去把油耗尽
{
tank.cost+=((buf[index].d-buf[i].d)/Davg-tank.left)*buf[i].p;
tank.left=0;
i=index;
tank.d=buf[i].d;
}
else //否则,把油加满,然后找到满油箱可到达范围内次低价站
{
tank.cost+=(Cmax-tank.left)*buf[i].p;
tank.left=Cmax;
double low=1000000;
for (int j=i+1;j<=n&&buf[j].d<=buf[i].d+farest;++j)
if (buf[j].p<low)
{
index=j;
low=buf[j].p;
}
if (index!=i) //对应上述状态3,开到次低价站
{
tank.left-=(buf[index].d-buf[i].d)/Davg;
i=index;
tank.d=buf[i].d;
}
else break; //对应上述状态4,没有下一站
}
}
}
if (i==n) printf("%.2lf\n", tank.cost);
else printf("The maximum travel distance = %.2lf\n", tank.d+tank.left*Davg);
}
}
return 0;
}
https://blog.csdn.net/u013457107/article/details/18365097