题目1437：To Fill or Not to Fill

2017/03/18 15:24

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 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
50 1300 12 2
7.10 0
7.00 600```

```749.17
The maximum travel distance = 1200.00```

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
double Cmax = scan.nextInt();
double D = scan.nextInt();
double Davg = scan.nextDouble();
int N = scan.nextInt();
if (Cmax > 100 || D > 30000 || Davg > 20 || N > 500) {
continue;
}
List<Station> stationList = new ArrayList<Station>();
for (int i = 0; i < N; i++) {
}
Collections.sort(stationList);
double maximum = 0.00, price = 0.00, oil = 0.00, availableDis = Cmax * Davg;
int cheapIndex = 0;
Station cheapStation;
// 循环结束flag为true
boolean flag = false;
if (N == 0 || stationList.get(0).dis != 0) {
System.out.println("The maximum travel distance = 0.00");
continue;
}
for (int i = 0; i < N && !flag;) {
cheapStation = null;
cheapIndex = -1;
// 查找经过的最便宜加油站，若经过的加油站价格低于出发的加油站（即i）价格，直接结束循环
for (int j = i + 1; j < N; j++) {
// 判断加油站距离不大于满油能走的距离
if ((stationList.get(j).dis - stationList.get(i).dis <= availableDis)
&& stationList.get(j).dis < D) {
// 若参与比较的加油站，油价低于当前加油站，则结束循环
if (stationList.get(j).price < stationList.get(i).price) {
cheapStation = stationList.get(j);
cheapIndex = j;
break;
}
// 若参与比较的加油站为第一个，则记录为最便宜的加油站（cheapStation）
if (j == i + 1) {
cheapStation = stationList.get(j);
cheapIndex = j;
}
// 若参与比较的加油站为比较范围中最低价格，则覆盖原来的cheapStation
if (stationList.get(j).price < cheapStation.price) {
cheapStation = stationList.get(j);
cheapIndex = j;
}
} else {
break;
}
}
//1.可直接走到终点，当前位置最低价格或最后一个加油站
if (stationList.get(i).dis + availableDis >= D) {
// 若当前加油站为最后一个，或为最便宜的加油站，则直接在此处加油至终点
if (cheapStation == null || stationList.get(i).price <= cheapStation.price) {
oil = oil - (D - stationList.get(i).dis) / Davg;
if (oil < 0) {
price += (0 - oil) * stationList.get(i).price;
}
System.out.printf("%.2f\n", price);
flag = true;
break;
}
}
//2.不能到达终点，没有可以经过的加油站，计算加满油能走的最大距离
if (cheapStation == null) {
maximum = stationList.get(i).dis + availableDis;
System.out.printf("The maximum travel distance = %.2f\n",maximum);
flag = true;
break;
}
//3.不能到达终点，当前加油站为最便宜价格，加满油 ，开到后面经过的最便宜加油站
if(stationList.get(i).price<cheapStation.price){
price += (Cmax-oil)*stationList.get(i).price;
oil = Cmax;
//oil = Cmax - (cheapStation.dis-stationList.get(i).dis)/Davg;
}
//4.不能到达终点，适量加油，开到能经过的最便宜的加油站
oil = oil - (cheapStation.dis-stationList.get(i).dis)/Davg;
if(oil<0){
price+=(0-oil)*stationList.get(i).price;
oil = 0;
}
i = cheapIndex;
}
}
}
}

class Station implements Comparable<Station> {
double price;
double dis;

Station() {
}

Station(double price, double dis) {
this.price = price;
this.dis = dis;
}

@Override
public String toString() {
return "Station [price=" + price + ", dis=" + dis + "]";
}

@Override
public int compareTo(Station o) {
return (int) (dis - o.dis);
}
}

0
0 收藏

0 评论
0 收藏
0