[PAT]1033. To Fill or Not to Fill (25)(Java实现)

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



解法是参考某c++的解法,原链接: https://www.cnblogs.com/XBWer/p/3866486.html
但是java有些测试样例无法通过,个人认为是double精度问题。
暂时不知道如果解决,如果知道解决方法,欢迎留言讨论


package go.jacob.day1129;

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

/*
 * 处在当前加油站(起点加油站)的情况下
 * 情况1:600米范围内,有目的地——计算恰好能到目的地的油量                            
 * 情况2:600米范围内没有加油站,无论油价多贵——加满——能跑多远算多远                                                
 * 情况3:600米范围内有加油站:                                                                         
 * a:有比当前加油站的油价更便宜的加油站——加到恰好能到那个最近的油站的油量        
 * (注:1、如果有多个便宜的,还是要先选取最近的那个,而不是最便宜的那个;2、可能还有油剩余)
 * b:没有比当前加油站的油价更便宜的加油站——加满,然后在600范围内找到最便宜的加油站加油 
 */

/**
 * 参考解法:https://www.cnblogs.com/XBWer/p/3866486.html
 * 
 * @author Administrator 贪心算法求解
 */
public class Demo2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		double tank = sc.nextDouble(), distance = sc.nextDouble(), avgDist = sc.nextDouble();
		int n = sc.nextInt();
		List<Station> list = new ArrayList<Station>();
		for (int i = 0; i < n; i++) {
			Station s = new Station(sc.nextDouble(), sc.nextInt());
			list.add(s);
		}
		sc.close();
		// 对Station的位置进行排序
		Collections.sort(list);
		// 特殊情况:1.目的地址为原点;2.原点没有加油站
		if (distance == 0) {
			System.out.println("0.00");
			return;
		}
		if (list.get(0).getDist() != 0) {
			System.out.println("The maximum travel distance = 0.00");
			return;
		}
		int curStNum = 0;// 当前所处加油站编号
		double curGas = 0;// 当前的油量
		double curCost = 0;// 当前花费
		boolean flag = false;// 是否可以到达目的地
		double maxDis = tank * avgDist;
		while (!flag) {
			boolean tag = false;// 最大距离内是否有加油站
			boolean ifCheaper = false;// 是否有便宜的
			double cheapestPrice = 10000;// 找出最便宜的
			int cheapestNum = 0;// 没有更便宜的情况下,找出最便宜的
			for (int i = curStNum + 1; i < n; i++) {
				// 在可达范围内有加油站
				if ((list.get(i).getDist() - list.get(curStNum).getDist()) <= maxDis) {
					tag = true;// 有加油站
					if (list.get(i).getPrice() < list.get(curStNum).getPrice()) {// 情况3-a:在可达范围内有比当前更便宜的加油站
						ifCheaper = true;
						double dist = list.get(i).getDist() - list.get(curStNum).getDist();
						double needGas = dist / avgDist - curGas;
						curGas = 0;
						curCost += needGas * list.get(curStNum).getPrice();
						curStNum = i;
						break;
					}
					if (list.get(i).getPrice() < cheapestPrice) {
						cheapestPrice = list.get(i).getPrice();
						cheapestNum = i;
					}
				} else// 范围内没有加油站
					break;
			}
			// (没有更便宜的加油站)说明已经可以到达目的地了,情况1
			if (!ifCheaper && (distance - list.get(curStNum).getDist() <= maxDis)) {
				double dist = distance - list.get(curStNum).getDist();
				double needGas = dist / avgDist - curGas;
				curCost += needGas * list.get(curStNum).getPrice();
				System.out.printf("%.2f", curCost);
				return;
			}
			if (tag && !ifCheaper) {// 情况3-b:没有比当前加油站的油价更便宜的加油站——加满,然后在600范围内找到最便宜的加油站加油
				double needGas = tank - curGas;
				curCost += (needGas * list.get(curStNum).getPrice());
				double dist = list.get(cheapestNum).getDist() - list.get(curStNum).getDist();
				curGas = tank - dist / avgDist;
				curStNum = cheapestNum;
			} else if (!tag) {
				System.out.print("The maximum travel distance = ");
				System.out.printf("%.2f", list.get(curStNum).getDist() + maxDis);
				return;
			}
		}
	}

	/*
	 * 加油站类
	 */
	static class Station implements Comparable<Station> {
		private double price;
		private double dist;

		public Station(double price, double dist) {
			super();
			this.price = price;
			this.dist = dist;
		}

		@Override
		public int compareTo(Station s1) {
			return this.dist - s1.getDist() < 0 ? -1 : 1;
		}

		public double getPrice() {
			return price;
		}

		public double getDist() {
			return dist;
		}

	}
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值