☆1033

 思路很简单,查找可到达范围内最便宜的加油站,如果没有找到加油站直接返回当前加油站的距离+满油的里程,如果找到了对比改加油站的油价和当前加油站的油价。

  有 3点需要注意

  1. 添加终点到加油站列表里,并设改点油价为0
  2. 初试状态为,nowsta=0; cost=0; nowTank=0; ,不设当前距离等其他变量,减少混淆概率
  3. 找到加油站后,直接更新为到此加油站的状态。

  代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn=510;
 6 const int INF=100000000;
 7 struct station{
 8     double price, dis;
 9 }sta[maxn];
10 
11 
12 bool cmp(station a, station b){
13     return a.dis<b.dis;
14 }
15 int main()
16 {
17     double capacity, distance, avg;
18     int num;
19     cin>>capacity>>distance>>avg>>num;
20     for(int i=0;i<num;i++)cin>>sta[i].price>>sta[i].dis;
21     sta[num].price=0, sta[num].dis=distance;
22     sort(sta,sta+num,cmp);
23 
24 
25 
26 
27     if(sta[0].dis>0){cout<<"The maximum travel distance = 0.00";}
28     else{
29         int now=0;
30         double cost=0, nowTank=0, MAX=capacity*avg;
31         while(now<num){
32             int k=-1;
33             double priceMin=INF;
34             for(int i=now+1;i<=num&&sta[i].dis-sta[now].dis<=MAX;i++){
35                 if(sta[i].price<priceMin){
36                     priceMin=sta[i].price;
37                     k=i;
38                 }
39                 if(sta[i].price<sta[now].price)break;
40             }
41             if(k==-1)break;
42             else{
43                 double need=(sta[k].dis-sta[now].dis)/avg;//用油
44                 if(priceMin<sta[now].price){
45                     if(nowTank<need){
46                         cost+=(need-nowTank)*sta[now].price;
47                         nowTank=0;
48                     }
49                     else{
50                         nowTank-=need;
51                     }
52 
53                 }else{
54                     cost+=(capacity-nowTank)*sta[now].price;
55                     nowTank=(capacity-need);
56                 }
57                 now=k;
58             }
59 
60         }
61             if(now==num){
62                 printf("%.2f\n",cost);
63             }else{
64                 printf("The maximum travel distance = %.2f",sta[now].dis+MAX);
65             }
66         }
67 
68     return 0;
69 }

 

posted on 2019-02-27 10:36  Vitavi  阅读(173)  评论(0编辑  收藏  举报

导航