You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

变种加油站任务的DP算法及最优低成本加油策略问询

最低成本加油最优算法:滑动窗口最小值解法

Alright, let's break down this classic road trip cost optimization problem step by step. Here's how you can use a sliding window minimum approach to lock in the lowest-cost route from point A to point B:

Core Problem Constraints

First, let's recap the key rules to make sure we're on the same page:

  • Total trip distance: l kilometers
  • Vehicle's max fuel range (full tank): b kilometers
  • n gas stations along the way, each only allows filling the tank to full (no partial fills)
  • Each station has a unique fuel cost per unit
  • Starting state: Tank is completely full at point A

Sliding Window Minimum Approach: How It Works

This method focuses on always making the cheapest possible fuel decision within your current reach, using a sliding window to track the best options. Here's the play-by-play:

  1. Preprocess All Stops
    First, list out all stops in order of their distance from point A: this includes point A, every gas station, and point B (set B's fuel cost to 0 since you won't need to refuel there). Each entry should have two values: distance_from_A and fuel_cost.

  2. Define the Sliding Window
    For your current position (let's say station i), the window includes every station j where distance[j] - distance[i] ≤ b. In plain terms: all stops you can reach on a full tank from your current location.

  3. Maintain a Min-Cost Queue
    Use a double-ended queue (deque) to keep track of stations in the window, sorted by fuel cost (cheapest first). This lets you instantly grab the lowest-cost option in your reach. Follow these rules to keep the queue valid:

    • When adding a new station to the window: Remove all stations from the end of the queue that have a higher fuel cost than the new station. These stations are irrelevant—you'd never choose to refuel at a more expensive, farther station when a cheaper, closer one exists. Then add the new station to the queue.
    • When a station falls outside your current window (you can't reach it anymore from your new position): Remove it from the front of the queue.
  4. Make Refueling Decisions
    At each stop, check the cheapest station in your current window (the front of the queue):

    • If that cheapest station has a lower cost than your current stop: Only buy enough fuel to reach that cheaper station (since it's better to fill up there). Then drive to it and fill the tank to full.
    • If all stations in the window are more expensive than your current stop: Fill your tank to full here (this is the cheapest option in your reach), then drive to the farthest possible station in the window (or until you hit a cheaper station).
    • Repeat this logic until you reach point B.

Quick Example to Illustrate

Let's use a simplified scenario to see this in action:

Total trip distance l = 1000km, tank range b = 400km
Stops (distance from A, fuel cost):

  • A (0, $5) → starting with full tank
  • Station 1 (200km, $4)
  • Station 2 (500km, $6)
  • Station 3 (700km, $3)
  • B (1000km, $0)
  1. From A, your window covers 0-400km (includes Station 1). Since Station 1 is cheaper than A, you drive straight there (no need to refuel at A—you started full). Fill up to full at Station 1.
  2. From Station 1, your window covers 200-600km (includes Station 2). Station 2 is more expensive, so you fill up fully at Station 1, then drive to Station 2.
  3. From Station 2, your window covers 500-900km (includes Station 3). Station 3 is cheaper, so you drive directly there (your tank has enough range from 500 to 700km), then fill up fully.
  4. From Station 3, your window covers 700-1100km (includes B). B's "cost" is $0, so you drive straight to the end—no need to refuel again.

Why This Works

This approach runs in O(n) time complexity because each station is added to and removed from the queue exactly once. It's efficient even for large numbers of gas stations, and ensures you always make the most cost-effective choice at every step.

内容的提问来源于stack exchange,提问作者KarlR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:11:07