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

该博客探讨了如何使用贪心算法解决旅行规划问题,以最小化从城市A到城市B的旅行费用。问题涉及到汽车的油箱容量、每公升汽油的行驶里程、沿途加油站的位置和油价。博客提供了数据输入格式,并提到了参考书籍《计算机算法设计与分析》。

4-26 旅行规划问题


问题描述

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

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

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


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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值