• 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:

    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

    #include<iostream>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int inf=99999999;
    struct station{
       double price,dis;
    };
    bool cmp(station a,station b){
       return a.dis<b.dis;
    }
    int main(){
       double cmax,d,davg;
       int n;
       cin>>cmax>>d>>davg>>n;
       vector<station> sta(n+1);
       //sta[0]={0.0,d};//目的地
       sta[0].price=0.0;
       sta[0].dis=d;
       for(int i=1;i<=n;i++)
          cin>>sta[i].price>>sta[i].dis;
       sort(sta.begin(),sta.end(),cmp);
       double nowdis=0.0,maxdis=0.0,nowprice=0.0,totalprice=0.0,leftdis=0.0;
       if(sta[0].dis!=0){
          cout<<"The maximum travel distance = 0.00";
          return 0;
       }
       else{
          nowprice=sta[0].price;
       }
       while(nowdis<d){
          maxdis=nowdis+cmax*davg;
          double minPriceDis=0,minPrice=inf;
          int flag=0;
          for(int i=1;i<=n&&sta[i].dis<=maxdis;i++){
             if(sta[i].dis<=nowdis)continue;//跳过小于当前站点的加油站
             if(sta[i].price<nowprice){
                totalprice+=(sta[i].dis-nowdis-leftdis)*nowprice/davg;
                leftdis=0.0;
                nowprice=sta[i].price;
                nowdis=sta[i].dis;
                flag=1;
                break;
             }
             if(sta[i].price<minPrice){
                minPrice=sta[i].price;
                minPriceDis=sta[i].dis;
             }
          }
          //找不到比当前更低的价格,就找尽可能低的加油站
          //在当前加满油,保证最大距离用最便宜的油
          if(flag==0&&minPrice!=inf){
             totalprice+=(nowprice*(cmax-leftdis/davg));
             leftdis=cmax*davg-(minPriceDis-nowdis);
             nowprice=minPrice;
             nowdis=minPriceDis;
          }
          if(flag==0&&minPrice==inf){
             nowdis+=cmax*davg;
             printf("The maximum travel distance = %.2f", nowdis);
             return 0;
          }
       }
       printf("%.2f", totalprice);
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
  • 相关阅读:
    自用文章汇总
    Python+unittest+requests接口自动化测试框架搭建 完整的框架搭建过程
    血压心电的测量小工具,轻松了解身体状况,dido Y1S手环上手
    七日算法先导(签到)
    OpenGL笔记九之彩色三角形与重心插值算法
    【数学建模】图论模型(基础理论+最大流与最小费用流问题)
    【Rust指南】基础语法|基本数据类型|复合数据类型
    大数据讲课笔记1.1 安装配置CentOS
    【Qt炫酷动画】2.QAbstractAnimation抽象动画类
    厨房燃气安全新保障:红外点式可燃气体报警器的作用
  • 原文地址:https://blog.csdn.net/weixin_49047177/article/details/125609175