1033. To Fill or Not to Fill (25)

102 篇文章 0 订阅
29 篇文章 0 订阅

1033. To Fill or Not to Fill (25)

时间限制
10 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

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
油箱容量Cmax  要到距离的D的地方 每单位油能行Davg单位   这一路上有N个加油站
油单价 距离起始点
……
一开始没有油,所以距离出发点 0 一定要有加油站;
sort按照由近到远距离排序;
排除出发点没有加油站以后;
对于当前站点index来说,
如果在有油~满油的范围内,有站点,在这些加油站中
               如果存在单价比当前少的站点中取最靠近当前站index的,那么当就只要加油到能够到达这一站choiceIndex就行;
               否则在单价比当前多的站点中去单价最少的mIndex,那么考虑一下当前站加满能否到达终点,能,直接到达终点,
                                                                                      否则,加满油,到下一站mIndex
 

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
8月03日 23:14答案正确251033C++ (g++ 4.7.2)1384datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确130812/12
1答案正确11803/3
2答案正确12482/2
3答案正确11802/2
4答案正确13082/2
5答案正确11803/3
6答案正确13841/1
#include<iostream>    
#include<algorithm>
#define NOFind -1
#define NOprice -1.f
using namespace std;
struct Stations
{
  float price;
  float distance;
};
bool STACmp(const Stations&A,const Stations&B)
{
  return A.distance < B.distance;
}
void Starealn(Stations*Sta,int N)
{ 
  for (int index = 0; index < N; index++) 
    scanf("%f%f", &Sta[index].price, &Sta[index].distance);  
}
void Drive(Stations*Sta, int N, float Cmax, float D, float Davg)
{
  int index, mIndex, choiceIndex, i;
  bool Gameover= true;
  float Total, mPrice, oil, maxDrive;
  Total = 0;
  index = 0;
  maxDrive = Cmax*Davg;
  oil = 0;
  while (Gameover&& index <N)/*0~N-1有N个加油站,把最后一个N置为目的地*/
  {
    choiceIndex = mIndex= NOFind;
    mPrice = NOprice;
    for (i = index + 1; i <= N&&Sta[i].distance - Sta[index].distance <= maxDrive; i++)
    {
      if (NOprice == mPrice || mPrice>Sta[i].price)
      {
        mPrice = Sta[i].price;
        mIndex = i;
      }
      if (NOFind == choiceIndex&&Sta[index].price > Sta[i].price) 
        choiceIndex = i; 
    }
    if (NOFind == mIndex)
    {
      Gameover = false;/*满油也到不了下一站咯,离终点无望了*/ 
    }
    else 
      if (NOFind != choiceIndex)
    {
      Total += ((Sta[choiceIndex].distance - Sta[index].distance) / Davg - oil)*Sta[index].price;
      oil = 0;
      index = choiceIndex;/*下一站choiceIndex的单价比当前index低,显然加够从index到choiceIndex的油就好*/
    }
    else
    {
      if (Sta[index].distance + maxDrive < D)
      { 
           Total += (Cmax - oil)*Sta[index].price;
        oil = Cmax - (Sta[mIndex].distance - Sta[index].distance) / Davg;
        index = mIndex;/*从index加油能走到的下面几站的单价最少的还是比较贵,而即使加满油也不能到达目的地,所以就要加满油*/
      }
      else
      {  
              Total += ((D - Sta[index].distance) / Davg - oil)*Sta[index].price; 
        index = N;/*从index加满油能到达目的地,所以就要够到达目的地的油就好了*/
      }
    } 
  }  
  Sta[0].price = Total; 
  Sta[0].distance = Sta[index].distance == D ? D : Sta[index].distance + maxDrive; 
}
int main()
{
  int  N;
  float Cmax, D, Davg;
  Stations*Sta; 
  scanf("%f%f%f%d", &Cmax, &D,&Davg,&N);
  Sta = new Stations[N+1];
  Sta[N].price = 0;
  Sta[N].distance = D;
  Starealn(Sta, N); 
  sort(Sta, Sta + N, STACmp);
  if (0 == Sta[0].distance)
  {
    Drive(Sta, N, Cmax, D, Davg);
    if (Sta[0].distance<D)printf("The maximum travel distance = %.2f\n",Sta[0].distance);
    else  printf("%.2f\n", Sta[0].price); 
  }
  else printf("The maximum travel distance = 0.00\n");
  delete[]Sta;
  system("pause");
  return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值