1033. To Fill or Not to Fill (25)

本文介绍了一种算法,用于计算从杭州出发到达目的地城市的最经济路线。考虑到油箱容量、每单位油可行驶距离及沿途加油站的价格和位置,该算法能找出最省钱的行车方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1033. To Fill or Not to Fill (25)
时间限制 100 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

提示

该题目所要解决的问题是:给定若干加油站信息,问能否驾驶汽车行驶一定的距离。如果能够行驶完全程,则计算最小花费。若不能行驶完全程,则最远能够行驶多长距离。

拿到这一题,首先判断汽车是否能够行驶到终点。什么情况下汽车无法行驶到终点呢?两种情况:起点根本就没有加油站,汽车无法启动;或者中途两个加油站之间的距离大于加满油后汽车能够行驶的最大距离。前者汽车行驶的最大距离为0.00,而后者最大距离为当前加油站的距离加上在这个加油站加满油后能够行驶的最大距离。在这里,需要将加油站按到杭州的距离从小到大排序。

接下来在能够行驶到终点的情况下计算最小花费。我们首先从路程来考虑,如果在路上,我们能够使用最便宜的汽油,当然就在那个加油站加油了。所以从起点开始遍历每个加油站。假设遍历到了第i个加油站,我们现在来判断在加油站i应该加多少油。设当前汽车离杭州的距离为curLen,当前加油站离杭州的距离为nodes[i].dis,加满油以后汽车能够行驶的最大距离为(dis=cmax*len)。这样就有node[i].dis <= curLen <= nodes[i].dis+dis,否则的话第i个加油站的油是不起作用的。于是在第i个加油站的作用范围内寻找有没有更为便宜的加油站,如果有,则下次使用这个加油站的油(j),这次汽车应该行驶到这个加油站,即touch=nodes[j].dis。如果没有找到更为便宜的加油站则可以在第i个加油站加满油,即touch=nodes[i].dis+dis。然后判断下次应该行驶到的距离与当前距离的关系,如果下次应当行驶到的距离大于当前距离,则汽车行驶,否则不动(也就是说上个加油站的油更便宜,后一个加油站也便宜,根本就不需要在当前加油站加油)。

需要注意的是:在Pat上不用考虑同距离多个站点不同价格的情况,第一次写的在pat上都跑过了,所以应该不存在同距离不
同站点价格不同的情况,但是相同代码在codeup上跑不过,考虑以上情况后跑过。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;

typedef struct
{
    double price;
    double des;
}Route;

bool cmpByDes(Route a, Route b)
{
    if (a.des != b.des) return a.des < b.des;
    return a.price < b.price;
}

int main()
{
#ifdef _DEBUG
    //freopen("data.txt", "r+", stdin);
    fstream cin("data.txt");
#endif // _DEBUG

    const int MaxN = 510;
    int C, D, Davg, N, i ,nextStaId;
    Route route[MaxN];
    double cost, Rdes, CC , tmp;
    bool full = true;
    while (cin >> C >> D >> Davg >> N)
    {
        for (i = 0; i < N; ++i)
            cin >> route[i].price >> route[i].des;
        route[i++].des = D;//最后一个站点,为目的点

        sort(route, route + N, cmpByDes);
        if (route[0].des != 0)
        {
            printf("The maximum travel distance = 0.00\n");
            continue;
        }
        for (Rdes = 0, cost = 0, i = 0, CC = 0; Rdes < D && i < N;)
        {
            //寻找当前加油站满载行驶距离内的价格比当前站点便宜的最近的站点;
            if (i + 1 <= N && route[i + 1].des - route[i].des > C* Davg )
            {
                full = false;
                Rdes += C * Davg;
                break;
            }

            nextStaId = i;
            for (int j = i + 1; route[j].des - route[i].des <= C * Davg && j < N; ++j)
            {
                if (route[j].price < route[i].price)
                {
                    nextStaId = j;
                    break;
                }
            }

            if (nextStaId == i)//说明之后的站点价格都比现在的贵,所以满载
            {
                for (nextStaId = i + 1; nextStaId <= N && Rdes < D && route[nextStaId].des == route[i].des; ++nextStaId);
                if ((D - route[i].des) / Davg < C)
                {
                    cost += ((D - route[i].des) / Davg - CC) * route[i].price;
                    break;
                }
                cost += (C - CC) * route[i].price;
                CC = C - (double)(route[nextStaId].des - route[i].des) / Davg;
                Rdes = route[nextStaId].des;
                i = nextStaId;
            }
            else//加油或者不加油,走到下一个站点
            {
                double len = (double)(route[nextStaId].des - route[i].des) / Davg;
                cost += (len - CC) >= 0 ? (tmp = CC, CC = 0, (len - tmp) * route[i].price) : (CC -= len, 0.0);
                Rdes = route[nextStaId].des;
                i = nextStaId;
            }
        }

        full ? (printf("%.2f\n",cost)) : (printf("The maximum travel distance = %.2f\n", Rdes));
    }


#ifdef _DEBUG
    cin.close();
#ifndef _CODEBLOCKS
    system("pause");
#endif // !_CODEBLOCKS
#endif // _DEBUG

    return 0;
}

/**************************************************************
    Problem: 2031
    User: Sharwen
    Language: C++
    Result: 升仙
    Time:3 ms
    Memory:1712 kb
****************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值