Parcel-delivery app~45 min2s · 256 MBtarget O((n × battery + m × battery) log) time, O(n × battery) spaceshortest-paths · dijkstra
A warehouse uses a robot to carry parcels between stations. The aisles are two-way. Driving down an aisle takes as many seconds as its length, and uses as many units of battery. The robot starts with a full battery.
Some stations have a charger. Charging fills the battery completely and takes a fixed time. The team wants the fastest trip from a start station to a goal station.
Write fastest_route(n, edges_a, edges_b, lengths, chargers, battery, charge_time, start, goal).
0 to n - 1. Aisle i joins edges_a[i] and edges_b[i] in both directions. Driving it takes lengths[i] seconds and uses lengths[i] units of battery. The robot cannot start an aisle if it has less battery than the length of the aisle.start with battery units, which is a full battery.chargers[s] = 1, the robot may charge. Charging takes charge_time seconds and fills the battery to battery. It may charge as often as it likes, but only at those stations.goal, or -1 if it cannot.Input: n = 6, edges_a = [0, 1, 0, 3, 4, 2, 1], edges_b = [1, 2, 3, 4, 2, 5, 4], lengths = [4, 5, 3, 6, 3, 4, 2], chargers = [0, 1, 0, 0, 1, 0], battery = 8, charge_time = 5, start = 0, goal = 5
Output: 18The trace shows the fastest trip, step by step. Stations 1 and 4 have chargers.
| Step | What the robot does | Charge after | Time after |
|---|---|---|---|
| 1 | drive from 0 to 1 (aisle length 4) | 4 | 4 |
| 2 | drive from 1 to 4 (aisle length 2) | 2 | 6 |
| 3 | charge at 4 (takes 5) | 8 | 11 |
| 4 | drive from 4 to 2 (aisle length 3) | 5 | 14 |
| 5 | drive from 2 to 5 (aisle length 4) | 1 | 18 |
The shortest way, 0, 1, 2, 5, takes 13 seconds, but it needs 13 units of battery, and the robot has 8. The fastest trip charges once, at station 4, and takes 18 seconds.
Input: n = 2, edges_a = [0], edges_b = [1], lengths = [5], chargers = [0, 0], battery = 4, charge_time = 3, start = 0, goal = 1
Output: -1The only aisle needs 5 units of battery, and the robot has 4. There is no charger to help, so the output is -1.
Input: n = 3, edges_a = [0, 1], edges_b = [1, 2], lengths = [3, 3], chargers = [0, 1, 0], battery = 4, charge_time = 0, start = 0, goal = 2
Output: 6The robot drives 3 seconds to station 1, uses 3 of its 4 units, and charges for free there. Then it drives 3 more seconds to station 2. The output is 6.
2 ≤ n ≤ 1,000 (stations); 1 ≤ edges_a.length ≤ 5,000 (aisles); edges_b and lengths have the same length1 ≤ lengths[i] ≤ 100; 0 ≤ edges_a[i], edges_b[i] < nchargers.length = n, and each value is 0 or 11 ≤ battery ≤ 100; 0 ≤ charge_time ≤ 1,000 (seconds)0 ≤ start, goal < n; start and goal are differentThe robot's situation is a station and the battery it has left. That is at most 1,000 × 101 = 101,000 situations. Each station has a limited number of aisles, so the transitions between situations number at most 5,000 × 2 × 101 plus the charging steps. Dijkstra over these situations is fast, and plain Dijkstra over stations alone cannot see the battery.