算法设计与分析: 4-26 旅行规划问题

4-26 旅行规划问题


问题描述

G 先生想独自驾驶汽车从城市 A 到城市 B。从城市 A 到城市 B 的距离为 d 0 公里。汽车油箱的容量为 c 公升。每公升汽油能行驶 e 公里。出发点每公升汽油的价格为 p 元。从城市 A 到城市 B 沿途有 n 个加油站。第 i 个加油站距出发点的距离为 d i ,油价为每公升 p i 元。如 何规划才能使旅行的费用最省。

对于给定的 d 0 ,c,e,p,和 n 以及 n 个加油站的距离和油价 d i p i ,编程计算最小的旅行费用。如果无法到达目的地,输出“No Solution”。

数据输入:
第 1 行是 d 0 ,c,e,p,和 n。接下来的 n 行中每行 2 个数 d i p i


Java

import java.util.Scanner;

public class LvXingGuiHua {

    private static double totalDist, capacity, distPerLiter, startPrice;
    private static int n;

    private static double maxDriveDist, cost;
    private static boolean canReach;
    private static double MAX = 999999.99;

    private static double[] dist,price;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        while (true) {
            totalDist = input.nextDouble();
            capacity = input.nextDouble();
            distPerLiter = input.nextDouble();
            startPrice = input.nextDouble();
            n = input.nextInt();

            dist = new double[n+2];
            price = new double[n+2];

            //始点
            dist[0] = 0;
            price[0] = startPrice;

            for(int i=1; i<=n; i++){
                dist[i] = input.nextDouble();
                price[i] = input.nextDouble();
            }

            //终点
            dist[n+1] = totalDist;
            price[n+1] = 0;

            maxDriveDist = capacity * distPerLiter;
            cost = 0.0;
            canReach = true;

            for (int i=1; i<n+2; i++)
                if(dist[i]-dist[i-1] > maxDriveDist)
                    canReach = false;

            if (!canReach) {
                System.out.println("No Solution!");
            } else {
                driving();
                System.out.println(String.format("%.2f",cost));
            }
        }
    }


    private static int minPriceStation(int currentStation) {
        double minPrice = MAX;
        int nextStation = currentStation + 1;

        for (int i=currentStation+1; i <=n+1 && (dist[i]-dist[currentStation])<=maxDriveDist; i++) {
            if (price[i] < price[currentStation]) {
                nextStation = i;
                break;
            }

            if (price[i] < minPrice) {
                minPrice = price[i];
                nextStation = i;
            }
        }

        return nextStation;
    }

    private static void driving() {
        double rest = 0.0;
        double driveDist;
        double tmpCost;
        int currentStation = 0;
        int nextStation;

        while (currentStation <= n) {
            nextStation = minPriceStation(currentStation);
            driveDist = dist[nextStation] - dist[currentStation];
            if (price[nextStation] < price[currentStation]) {
                tmpCost = (driveDist / distPerLiter - rest) * price[currentStation];
                cost += tmpCost;
                rest = 0.0;
            } else {
                tmpCost = (capacity - rest) * price[currentStation];
                rest = capacity - driveDist / distPerLiter;
                cost += tmpCost;
            }

            currentStation = nextStation;
        }
    }
}

Input & Output

275.6 11.9 27.4 2.8 2
102.0 2.9
220.0 2.2
26.95

Reference

王晓东《计算机算法设计与分析》

猜你喜欢

转载自blog.csdn.net/ioio_/article/details/81086893