zoukankan      html  css  js  c++  java
  • A1033

    找出最小开销。

    思路:

    出发点的加油站编号设为0,终点的加油站编号设为n,其他加油站编号按距离依次排序。

    如果0号加油站的距离!=0,则无法出发,行驶距离为0.

    从起点开始,寻找规则为,如果存在油价小于本加油站的油价的,则计入,

    没有就计入油价最低的。

    如此循环,如果能到达终点,输出总花销;不能,输出总行驶距离。

    ps:输出的字符的拼写不能有误。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 using namespace std;
     5 const int INF=1000000000;
     6 const int maxn=510;
     7 struct station{
     8     double price,dis; //价格、与起点的距离 
     9 }st[maxn];
    10 bool cmp(station a,station b){
    11     return a.dis<b.dis; //按距离从小到大排序 
    12 }
    13 int main(){
    14     int n;
    15     double cmax,d,davg;
    16     scanf("%lf%lf%lf%d",&cmax,&d,&davg,&n);
    17     for(int i=0;i<n;i++){
    18         scanf("%lf%lf",&st[i].price,&st[i].dis);
    19     }
    20     st[n].price=0; //数组最后放置终点,价格为0
    21     st[n].dis=d; //终点距离为d
    22     sort(st,st+n,cmp); //所有加油站按距离从小到大排序
    23     if(st[0].dis!=0){
    24         printf("The maximum travel distance = 0.00
    ");
    25     } else{
    26         int now=0; //当前所处的加油站编号
    27         //总花费、当前油量、满油行驶最远距离
    28         double ans=0,nowTank=0,maxx=cmax*davg; 
    29         while(now<n){ 
    33             int k=-1;  // 最低油价加油站的编号 
    34             double priceMin=INF; // 最低油价
    35             for(int i=now+1;
    36              i<=n&&st[i].dis-st[now].dis<=maxx;i++){
    37                  if(st[i].price<priceMin){
    38                      priceMin=st[i].price;
    40                      k=i;
    43                     if(priceMin<st[now].price){
    44                         break;
    45                     }
    46                  }
    47              }
    48              if(k==-1) break;//满油状态无法找到加油站,退出循环 输出结果
    51             double need=(st[k].dis-st[now].dis)/davg;
    52             if(priceMin<st[now].price){
    55                 if(nowTank<need){
    56                     ans+=(need-nowTank)*st[now].price;
    57                     nowTank=0;
    58                 }else{
    59                     nowTank-=need;
    60                 }
    61             }else{//如果加油站k的油价高于当前油价 
    62                  ans+= (cmax-nowTank)*st[now].price;//将油箱加满
    63                  nowTank=cmax-need; 
    64             }
    65             now=k;//到达加油站k,进入下一个循环 
    66         }
    67         if(now==n){
    68             printf("%.2f
    ",ans);
    69         }else{
    70             printf("The maximum travel distance = %.2f
    ",st[now].dis+maxx);
    71         }
    72     }
    73     return 0;
    74 } 
  • 相关阅读:
    怎么继续程序员这条路
    深入理解Flash Player的应用程序域(Application Domains)(转载)
    网站策划未来趋势
    截取网页任意部分讨论
    如果你是一个有爱心的人或者即将成为一个有爱心的人 请投上一票吧
    prototype.js参考
    ASP.NETweb编程常用到的27个函数集
    C#实现web信息抓取
    12306火车票网站自动登录工具
    Flex和.net Webservice之间多层结构开发
  • 原文地址:https://www.cnblogs.com/Lynn-2019/p/10389609.html
Copyright © 2011-2022 走看看