zoukankan      html  css  js  c++  java
  • 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 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<cstdio>
    #include<algorithm>
    using namespace std;
    const int INF = 1000000000;
    struct gas{
        double dis,price;
    }st[510];
    bool cmp(gas a, gas b){
        return a.dis < b.dis; 
    }
    int main(){
        int N;
        double Cmax,D,Davg;//油箱容量,城市距离,每升汽油跑的距离,加油站数量
        scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&N);
        for(int i = 0; i < N; i++){
            scanf("%lf%lf",&st[i].price,&st[i].dis);
        } 
        st[N].price = 0;
        st[N].dis = D;
        sort(st,st+N,cmp);
        if(st[0].dis != 0){
            printf("The maximum travel distance = 0.00
    ");
        }else{
            int now = 0;
            double ans = 0, nowTank = 0, maxDis = Cmax * Davg;//总花费,当前油箱油量,满油箱跑的距离 
            while(now < N){
                int k = -1; //最低油价加油站编号 
                double minPrice = INF;
                for(int i = now+1; i <= N && st[i].dis - st[now].dis <= maxDis;i++){
                    if(st[i].price < minPrice){
                        minPrice = st[i].price;
                        k = i;
                    }
                    if(minPrice < st[now].price){
                        break;
                    }
                } 
                if(k == -1) break;
                double need = (st[k].dis - st[now].dis) /Davg; // 开过去所需的油量
                if(minPrice < st[now].price){ //当前加油站的油价高 
                     if(nowTank < need){  //油箱的油不够到达地点 
                         ans += st[now].price * (need - nowTank);
                         nowTank = 0;//到达地点后油箱空 
                     }else{
                         nowTank -= need;
                     }
                } else{     //当前加油站油价低 
                    ans += (Cmax - nowTank) * st[now].price;
                    nowTank = Cmax - need;
                }
                    now = k;
            }
            if(now == N) printf("%.2f
    ",ans);
          else printf("The maximum travel distance = %.2f
    ",st[now].dis+maxDis);
        
        }
    
        return 0;
    
    }
  • 相关阅读:
    TimeVal类——Live555源码阅读(一)基本组件类
    QT国际化 一 (lupdate/linguits/lrelease)
    qmake的使用
    将Apache加入到linux系统service
    ubuntu下编译VLC
    Ubuntu14.04安装intel集显驱动
    net-snmp的dateandtime数据类型
    MySQL的主从复制原理
    【iOS开发-25】UIDevice查看系统信息,从一个问题開始怎样高速找到自己想要的属性和方法并看懂它
    POJ 3279 Fliptile (二进制+搜索)
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/8657858.html
Copyright © 2011-2022 走看看