PATA 1033 To Fill or Not to Fill

举报
陈沧夜 发表于 2022/05/01 00:25:56 2022/05/01
【摘要】 1033 To Fill or Not to Fill  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...

1033 To Fill or Not to Fill 

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: C​max​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤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: P​i​​, the unit gas price, and D​i​​ (≤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:


   
  1. 50 1300 12 8
  2. 6.00 1250
  3. 7.00 600
  4. 7.00 150
  5. 7.10 0
  6. 7.20 200
  7. 7.50 400
  8. 7.30 1000
  9. 6.85 300

Sample Output 1:

749.17

  

Sample Input 2:


   
  1. 50 1300 12 2
  2. 7.10 0
  3. 7.00 600

Sample Output 2:

The maximum travel distance = 1200.00
  

待解决,思考中 

这个题目我想了好几天,知道是使用贪心算法但是对于严谨性一直欠考虑。

以下是《算法笔记》AC代码:

 


  
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const int maxn = 510;
  7. const int INF = 1000000000;
  8. struct station{
  9. double price,dis;//价格,与起点的距离
  10. }st[maxn];
  11. bool cmp(station a, station b)
  12. {
  13. return a.dis<b.dis;//从小到大排序
  14. }
  15. int main()
  16. {
  17. int n;
  18. double Cmax,D,Davg;
  19. scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&n);
  20. for(int i=0;i<n;i++)
  21. {
  22. scanf("%lf%lf",&st[i].price,&st[i].dis);
  23. }
  24. st[n].price = 0;//终点
  25. st[n].dis = D;//终点距离
  26. sort(st,st+n,cmp);//加油站从小到大排序
  27. if(st[0].dis!=0)//第一个加油站不为0,无法前进
  28. {
  29. printf("The maximum travel distance = 0.00\n");
  30. }
  31. else{
  32. int now = 0;//当前所处的加油站编号
  33. double ans =0, nowTank=0, MAX = Cmax*Davg;
  34. while(now<n) {
  35. //每次循环选出下一个需要到达的加油站
  36. int k=-1;
  37. double priceMin=INF;
  38. for(int i=now+1;i<=n&&st[i].dis-st[now].dis<=MAX;i++)
  39. {
  40. if(st[i].price<priceMin)
  41. {
  42. priceMin = st[i].price;
  43. k = i;
  44. if(priceMin<st[now].price)
  45. {
  46. break;
  47. }
  48. }
  49. }
  50. if(k == -1) break;
  51. double need = (st[k].dis-st[now].dis)/Davg;
  52. if(priceMin < st[now].price)
  53. {
  54. if(nowTank<need)
  55. {
  56. ans+=(need-nowTank)*st[now].price;
  57. nowTank = 0;
  58. }
  59. else{
  60. nowTank -= need;
  61. }
  62. }
  63. else{
  64. ans+=(Cmax-nowTank)*st[now].price;
  65. nowTank = Cmax - need;
  66. }
  67. now= k;
  68. }
  69. if(now == n)
  70. {
  71. printf("%.2f\n",ans);
  72. }
  73. else{
  74. printf("The maximum travel distance = %.2f\n",st[now].dis+MAX);
  75. }
  76. }
  77. return 0;
  78. }

 

文章来源: blog.csdn.net,作者:沧夜2021,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/CANGYE0504/article/details/89553546

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。