1033. To Fill or Not to Fill (25)

来源:互联网 发布:淘宝卖家怎么加入花呗 编辑:程序博客网 时间:2024/06/04 19:45

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 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 27.10 07.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/121答案正确11803/32答案正确12482/23答案正确11802/24答案正确13082/25答案正确11803/36答案正确13841/1
#include<iostream>    #include<algorithm>#define NOFind -1#define NOprice -1.fusing 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
原创粉丝点击