首页 > 其他 > 详细

1033. To Fill or Not to Fill (25)

时间:2015-12-06 11:12:15      阅读:183      评论:0      收藏:0      [点我收藏+]
贪心算法的模拟题。

时间限制
10 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

 

自己写的

  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<stdio.h>
  5. #pragma warning(disable:4996)
  6. using namespace std;
  7. struct Station
  8. {
  9. double price;
  10. double dist;
  11. };
  12. vector<Station> s;
  13. bool cmp(Station a, Station b) {
  14. return a.dist < b.dist;
  15. }
  16. int main(void) {
  17. freopen("Text.txt", "r", stdin);
  18. int cmax, d, davg, n;
  19. cin >> cmax >> d >> davg >> n;
  20. for (int i = 0; i < n; i++) {
  21. Station temp;
  22. cin >> temp.price >> temp.dist;
  23. s.push_back(temp);
  24. }
  25. Station terminal;
  26. terminal.price = 99999;
  27. terminal.dist = d;
  28. s.push_back(terminal);
  29. sort(s.begin(), s.end(), cmp);
  30. if (s[0].dist != 0) {
  31. cout << "The maximum travel distance = 0.00";
  32. return 0;
  33. }
  34. double fee = 0, leftgas = 0;
  35. int maxdis = cmax*davg;
  36. int sta = 0;
  37. while (true)
  38. {
  39. int minIndex = -1;
  40. double min = -1;
  41. int cheaperIndex = -1;
  42. for (int i = sta+1; i<=n && s[i].dist - s[sta].dist <= maxdis; i++) {
  43. if (minIndex == -1 || s[i].price < min) {
  44. min = s[i].price;
  45. minIndex = i;
  46. }
  47. if (s[i].price < s[sta].price) {
  48. cheaperIndex = i;
  49. break;
  50. }
  51. }
  52. if (minIndex == -1 && cheaperIndex == -1)break;
  53. if (cheaperIndex == -1) {
  54. if (d <= s[sta].dist + maxdis) {
  55. fee += s[sta].price*(d - s[sta].dist - leftgas*davg) / davg;
  56. leftgas = 0;
  57. sta = n;
  58. }
  59. else{
  60. fee += s[sta].price*(cmax - leftgas);
  61. leftgas = cmax - (s[minIndex].dist-s[sta].dist)/davg;
  62. sta = minIndex;
  63. }
  64. }
  65. else {
  66. fee += s[sta].price*(s[cheaperIndex].dist - s[sta].dist-leftgas*davg) / davg;
  67. leftgas = 0;
  68. sta = cheaperIndex;
  69. }
  70. }
  71. if (sta == n) {
  72. printf("%.2f", fee);
  73. }
  74. else {
  75. printf("The maximum travel distance = %.2f",s[sta].dist+maxdis);
  76. }
  77. return 0;
  78. }

根据别人的代码写的
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <iostream>
  4. #pragma warning(disable:4996)
  5. using namespace std;
  6. struct Station {
  7. double price;
  8. double dis;
  9. };
  10. int cmp(const void* ta, const void* tb) {
  11. Station* a = (Station*)ta;
  12. Station* b = (Station*)tb;
  13. return a->dis - b->dis;
  14. }
  15. int main(void) {
  16. freopen("Text.txt", "r", stdin);
  17. int i, j, n;
  18. double cmax, d, davg;
  19. Station sta[510];
  20. cin >> cmax >> d >> davg >> n;
  21. for (int i = 0; i < n; i++) {
  22. cin >> sta[i].price >> sta[i].dis;
  23. }
  24. sta[n].price = 0;
  25. sta[n].dis = d;
  26. qsort(sta, n + 1, sizeof(Station), cmp);
  27. if (sta[0].dis != 0) {
  28. printf("The maximum travel distance = 0.00\n");
  29. return 0;
  30. }
  31. double maxdis = cmax*davg;
  32. int s = 0;
  33. double tfee = 0;
  34. double leftgas = 0;
  35. while (sta[s].dis<d)
  36. {
  37. double maxfee = -1;
  38. int maxindex = -1, fstindex = -1;
  39. for (i = s + 1; i <= n && (sta[i].dis - sta[s].dis <= maxdis); i++) {
  40. if (maxfee == -1 || (sta[i].price < maxfee)) {
  41. maxfee = sta[i].price;
  42. maxindex = i;
  43. }
  44. if (fstindex == -1 && (sta[i].price < sta[s].price)) {
  45. fstindex = i;
  46. }
  47. }
  48. if (maxindex == -1 && fstindex == -1)break;
  49. if (fstindex == -1) {
  50. if (d - sta[s].dis <= maxdis) {
  51. tfee += ((d - sta[s].dis) / davg - leftgas)*sta[s].price;
  52. leftgas = 0;
  53. s = n;
  54. }
  55. else {
  56. tfee += (cmax - leftgas)*sta[s].price;
  57. leftgas = cmax - (sta[maxindex].dis - sta[s].dis) / davg;
  58. s = maxindex;
  59. }
  60. }
  61. else {
  62. tfee += ((sta[fstindex].dis - sta[s].dis) / davg - leftgas)*sta[s].price;
  63. leftgas = 0;
  64. s = fstindex;
  65. }
  66. }
  67. if (sta[s].dis == d) printf("%.2lf\n", tfee);
  68. else
  69. printf("The maximum travel distance = %.2lf\n", sta[s].dis + maxdis);
  70. return 0;
  71. }





1033. To Fill or Not to Fill (25)

原文:http://www.cnblogs.com/zzandliz/p/5023122.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!