您的位置:首页 > 其它

PAT (Advanced Level) Practise 1033 To Fill or Not to Fill (25)

2017-07-22 10:41 399 查看


1033. To Fill or Not to Fill (25)

时间限制

100 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

ZHANG, Guochuan

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


题意:一辆车的油箱容量为V,他要从起点到终点去,两地相距s,一共有n个加油站,告诉你每个加油站距离起点的距离和加油需要的油费,每单位的有能行驶L,问这辆车到终点最少花多少钱,若到不了,最远能开到哪里

解题思路:这是一道贪心题,从一个点出发,如果能到一个油费比它低的点一定优先去这个点,如果没有则加满油开到油费最便宜的地方,如果还没有则记录下能开到的最远距离

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n;
double V, l, a, maxdis, mincost;

struct point
{
double p, d;
bool operator<(const point&a)const
{
return d == a.d ? p < a.p : d < a.d;
}
}x[509];

void dfs(int k, double cost, double rest)
{
maxdis = max(maxdis, x[k].d + V*a);
int p = k;
for (int i = k + 1; i < n; i++)
{
if (x[i].d - x[k].d <= V*a)
{
if (x[i].p <= x[k].p)
{
double xx = (x[i].d - x[k].d) / a;
if (xx > rest) dfs(i, cost + (xx - rest)*x[k].p, 0);
else dfs(i, cost, rest - xx);
return;
}
if (p == k || x[i].p < x[p].p) p = i;
}
}
if (x[k].d + V*a >= l)
{
if ((l - x[k].d) / a - rest >= 0)
mincost = min(mincost, cost + ((l - x[k].d) / a - rest)*x[k].p);
}
if (p > k)
{
double xx = (V - rest)*x[k].p;
dfs(p, cost + xx, V - (x[p].d - x[k].d) / a);
}
}

int main()
{
while (~scanf("%lf%lf%lf%d", &V, &l, &a, &n))
{
maxdis = 0, mincost = INF;
for (int i = 0; i < n; i++) scanf("%lf%lf", &x[i].p, &x[i].d);
sort(x, x + n);
if (x[0].d > 0) { printf("The maximum travel distance = 0.00\n"); continue; }
dfs(0, 0, 0);
if (maxdis < l) printf("The maximum travel distance = %.2lf\n", maxdis);
else printf("%.2lf\n", mincost);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: