PAT (Advanced Level) Practise 1033 To Fill or Not to Fill (25)
来源:互联网 发布:尚硅谷大数据视频 编辑:程序博客网 时间:2024/06/05 10:33
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 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 300Sample Output 1:
749.17Sample Input 2:
50 1300 12 27.10 07.00 600Sample 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 longconst 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;}
- PAT (Advanced Level) Practise 1033 To Fill or Not to Fill (25)
- PAT (Advanced Level) Practise 1033 To Fill or Not to Fill (25)
- 1033. To Fill or Not to Fill (25)【贪心+模拟】——PAT (Advanced Level) Practise
- PAT (Advanced) 1033. To Fill or Not to Fill (25)
- Pat(Advanced Level)Practice--1033(To Fill or Not to Fill)
- 【PAT Advanced Level】1033. To Fill or Not to Fill (25)
- 【C++】PAT (advanced level)1033. To Fill or Not to Fill (25)*
- 【PAT】【Advanced Level】1033. To Fill or Not to Fill (25)
- PAT (Advanced Level) 1033. To Fill or Not to Fill (25)最省加油方案,贪心算法
- pat 1033 To Fill or Not to Fill
- PAT 1033 To Fill or Not to Fill 贪心
- 【PAT 1033】To Fill or Not to Fill
- 1033. To Fill or Not to Fill (25)-PAT
- 【PAT】1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- PAT A 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)(贪心)
- PAT (Advanced Level) Practise 1032 Sharing (25)
- 不要重新定义继承来的非虚函数
- PHP实现无限极分类的两种方式,递归和引用
- 周志华点评机器学习会议
- 关于leveldb源码整理之一
- PAT (Advanced Level) Practise 1033 To Fill or Not to Fill (25)
- SPOJ-QTREE4 Query on a tree IV(边分治)
- tip-2017-7-20
- 常见的web漏洞及其防范
- SQL注入攻击与防御的一些姿势
- python文件打开参数区别
- Intellij-IDEA-maven+springMVC+mybatis整合DEMO
- sql的相关技术杂谈
- 台湾清华彭明辉教授的研究生手册