I - ドローン配送と高度制御 / Drone Delivery and Altitude Control Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
This problem asks for the minimum total time to travel from base \(1\) (at altitude \(S\)) to base \(N\) (at altitude \(T\)) by moving between bases and ascending/descending altitudes without exceeding a pre-configured altitude range \([U, V]\) (lower and upper bounds). Noticing that the maximum altitude \(K\) is very small (\(K \le 10\)), we can reduce this problem to brute-forcing the altitude range \([U, V]\) and solving a shortest path problem (Dijkstra’s algorithm) on a graph where each state is \((\text{base} \times \text{altitude})\).
Analysis
1. Focusing on the Constraints
The most notable constraint in this problem is \(K \leq 10\). Since the altitude can only take integer values between \(0\) and \(K\), inclusive, there are at most \(\frac{(K+1)(K+2)}{2} \leq 66\) possible choices for the altitude range \([U, V]\). Furthermore, because the range must include both the starting altitude \(S\) and the destination altitude \(T\), we only need to consider pairs \((U, V)\) satisfying \(0 \leq U \leq \min(S, T)\) and \(\max(S, T) \leq V \leq K\). This narrows down the number of candidates to at most around \(36\), which is extremely small.
2. Shortest Path Problem for a Fixed Altitude Range \([U, V]\)
When the altitude range \([U, V]\) is fixed, the adjustment cost of the device, \(G \times (V - U)\), becomes a constant. The remaining problem is to move and ascend/descend such that the altitude always stays within \([U, V]\).
Here, we define the drone’s state as the pair \((\text{current base } i, \text{current altitude } h)\). Since \(h \in [U, V]\), the number of states is at most \(N \times (K + 1) \leq 1000 \times 11 = 11000\). The transitions (edges) between states are as follows:
- Using a flight route: For flight route \(j\) (\(A_j \to B_j\)), if the current altitude \(h\) satisfies \(L_j \leq h \leq R_j\):
- State \((A_j, h) \to (B_j, h)\) with cost \(C_j + D_j \times |h - H_j|\)
- Ascending by \(1\) altitude: If \(h < V\):
- State \((i, h) \to (i, h + 1)\) with cost \(P_i\)
- Descending by \(1\) altitude: If \(h > U\):
- State \((i, h) \to (i, h - 1)\) with cost \(Q_i\)
Since all edge weights (movement and ascent/descent costs) are non-negative, the shortest distance from the start state \((1, S)\) to the goal state \((N, T)\) can be computed efficiently using Dijkstra’s algorithm.
3. Complexity and Pruning
Although running Dijkstra’s algorithm for all valid pairs \((U, V)\) is fast enough to pass, we can speed it up even further by ordering the search and applying pruning: - Search in ascending order of the range width \((V - U)\). - When the base cost \(G \times (V - U)\) is already greater than or equal to the current tentative minimum answer, terminate the search early since subsequent pairs cannot yield a better solution. - Inside Dijkstra’s algorithm, if the popped cumulative cost exceeds the tentative minimum answer, prune that branch.
Algorithm
- Preconstructing the Graph: Pre-generate the directed edges corresponding to flight routes for all altitudes \(h \in [0, K]\). States \((i, h)\) can be conveniently encoded into a single integer, such as \((i \ll 4) \mid h\).
- Enumerating Candidates for \((U, V)\): Enumerate all pairs \((U, V)\) satisfying \(0 \le U \le \min(S, T)\) and \(\max(S, T) \le V \le K\), and sort them in ascending order of width \((V - U)\).
- Shortest Path Search for Each Pair:
- Initialize the tentative minimum time as \(ans = \infty\).
- For each \((U, V)\), if the base cost \(G \times (V - U) \ge ans\), break out of the loop.
- Run Dijkstra’s algorithm starting from \((1, S)\).
- In Dijkstra’s algorithm, in addition to flight route transitions, include descent transitions if \(U < h\) and ascent transitions if \(h < V\).
- When reaching the destination \((N, T)\), update \(ans\) with \((\text{cost} + G \times (V - U))\).
- Output the Result:
If \(ans\) was updated, output its value; if it remains at its initial value, output
NAto indicate that the destination is unreachable.
Complexity
- Time Complexity: \(O(K^2 \cdot (N K + M K) \log(N K))\)
- Number of states (vertices) \(V' = O(N K)\), number of transitions (edges) \(E' = O(N K + M K)\)
- Time taken for a single run of Dijkstra’s algorithm is \(O(E' \log V')\)
- The number of \((U, V)\) candidates is at most \(O(K^2)\)
- Substituting \(N \leq 1000, M \leq 5000, K \leq 10\), the number of vertices is approximately \(1.1 \times 10^4\), the number of edges is approximately \(6.5 \times 10^4\), and there are at most \(36\) candidate pairs. With pruning also taking effect, this easily runs well within the time limit.
- Space Complexity: \(O(NK + MK)\)
- The memory required to store states, graph edges, and the distance array is very small, well within a few megabytes.
Implementation Details
Bit Representation of States: When packing base index \(i\) and altitude \(h\) into a single integer, since \(h \leq 10 < 16 = 2^4\), using
(i << 4) | hallows for fast bidirectional conversion using only bitwise operations.Precomputing Flight Routes: Regardless of the \([U, V]\) setting, each flight route has fixed passage conditions \(L_j \le h \le R_j\) and costs. Therefore, pre-generating flight route edges for all altitudes and only dynamically applying the range bounds for ascending/descending operations (\(h > U, h < V\)) inside Dijkstra’s algorithm avoids the overhead of rebuilding the graph each time.
Source Code
import sys
import heapq
def main():
input_data = sys.stdin.read().split()
if not input_data:
return
it = iter(input_data)
N = int(next(it))
M = int(next(it))
K = int(next(it))
G = int(next(it))
S = int(next(it))
T = int(next(it))
P = [0] * N
Q = [0] * N
for i in range(N):
P[i] = int(next(it))
Q[i] = int(next(it))
MAX_NODES = N << 4
flight_adj = [[] for _ in range(MAX_NODES)]
for _ in range(M):
u = int(next(it)) - 1
v = int(next(it)) - 1
L = int(next(it))
R = int(next(it))
C = int(next(it))
H = int(next(it))
D = int(next(it))
for h in range(L, R + 1):
cost = C + D * abs(h - H)
from_u = (u << 4) | h
to_u = (v << 4) | h
flight_adj[from_u].append((to_u, cost))
INF = 10**18
ans = INF
start_u = (0 << 4) | S
target_u = ((N - 1) << 4) | T
min_st = min(S, T)
max_st = max(S, T)
pairs = []
for U in range(0, min_st + 1):
for V in range(max_st, K + 1):
pairs.append((U, V))
# 高度範囲の幅が小さい順に探索することで枝刈りの効果を高める
pairs.sort(key=lambda p: p[1] - p[0])
for U, V in pairs:
base_cost = G * (V - U)
if base_cost >= ans:
break
dist = [INF] * MAX_NODES
dist[start_u] = 0
pq = [(0, start_u)]
while pq:
d, u = heapq.heappop(pq)
if d + base_cost >= ans:
break
if d > dist[u]:
continue
if u == target_u:
ans = d + base_cost
break
node = u >> 4
h = u & 15
# 航路による移動
for to_u, cost in flight_adj[u]:
nd = d + cost
if nd < dist[to_u]:
dist[to_u] = nd
heapq.heappush(pq, (nd, to_u))
# 拠点での高度を上げる操作
if h < V:
to_u = u + 1
nd = d + P[node]
if nd < dist[to_u]:
dist[to_u] = nd
heapq.heappush(pq, (nd, to_u))
# 拠点での高度を下げる操作
if h > U:
to_u = u - 1
nd = d + Q[node]
if nd < dist[to_u]:
dist[to_u] = nd
heapq.heappush(pq, (nd, to_u))
if ans >= INF:
print("NA")
else:
print(ans)
if __name__ == '__main__':
main()
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: