您的位置:首页 > 其它

To Fill or Not to Fill<2011浙大复试机试>

2013-03-12 11:04 190 查看
题目描述: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.

输入: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.

输出: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.

样例输入:50 1300 12 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 30050 1300 12 27.10 07.00 600

样例输出:749.17The maximum travel distance = 1200.00提示:此代码可在九度AC,但在pat有个样例一直无法通过,个人感觉可能与精度有关
最难理解的部分是next(),已注释

#include <stdio.h>
 #include <algorithm>
 #include <math.h>
 using namespace std;
 
 double sum, gl, maxd, maxdis, Cmax, D, per;
 bool available;
 int N;
 struct gas {
     double price, d;
     bool operator <(const gas &g) const {
         return d < g.d;
     }
 } buf[501];
 
 /*
 void next(站号){
     把站号设为下一站
     k = k + 1;
     if(不是第一站)
         计算剩余油量 = 该站与上一站距离差 / 每单位油行驶距离;
     for(从下一站开始查找更便宜的加油站,直到找到更便宜的或者到达最后一站即N + 1);
     if(找到的站距离当前站较远)
         加满油
     else{
         span = 从当前站到达找到的站的耗油量 - 还剩余的油量;
         if(span > 0)说明当前油量不足以到达查找到的站,需要加油
             加油;
     }
 }
 */
 void next(int i) {
     int k = i + 1;
     if (i != 1)
         gl -= (buf[i].d - buf[i - 1].d) / per;
     for (; k < N + 1 && buf[k].price >= buf[i].price; k++); 
     if ((int)(buf[k].d - buf[i].d) > (int)maxdis) {
         sum += (Cmax - gl) * buf[i].price;
         gl = Cmax;
     } else {
         double span = (buf[k].d - buf[i].d) / per - gl;
         if (fabs(span) > 1e-8 && span > 0) {
             sum += span * buf[i].price;
             gl = (buf[k].d - buf[i].d) / per; 
         }
     }
 }
 
 int main() {
     while (scanf("%lf%lf%lf%d", &Cmax, &D, &per, &N) != EOF) {
         maxd = sum = gl = 0;
         available = true;
         maxdis = Cmax * per;
         //加满一次油行驶最大距离
         for (int i = 1; i <= N; i++)
             scanf("%lf%lf", &buf[i].price, &buf[i].d);
         buf[N + 1].price = 1000000;    
         //初始化目的地价格
         buf[N + 1].d = D;
         sort(buf + 1, buf + N + 2);
 
         if (buf[1].d > 0) {
             printf("The maximum travel distance = 0.00\n");
             continue;
         }
 
         for (int i = 1; i <= N; i++) {
             next(i);
             //用于计算价格
             if (buf[i + 1].d - buf[i].d > maxdis) {
                 maxd = buf[i].d + maxdis;
                 available = false;
                 printf("The maximum travel distance = %.2lf\n", maxd);
                 break;
             }
         }
         if(available)
             printf("%.2lf\n", sum);
     }
     return 0;
 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐