Official

I - ドローン配送と高度制御 / Drone Delivery and Altitude Control Editorial by admin Editorial for printing

claude5.5opus-high

Overview

We try all possible altitude ranges \([U,V]\), and for each range, we find the shortest path on a graph where each vertex represents “(base, altitude)”. The answer is the minimum value of \(G(V-U)\) plus the shortest path length among all ranges.

Observations

Insight 1: Fixing the altitude range reduces it to a standard shortest path problem

If we fix \([U,V]\), the adjustment time \(G(V-U)\) becomes a constant. The only remaining condition is that “the altitude must always stay within \([U,V]\)”. This can be handled simply by not using any state whose altitude is outside this range.

Insight 2: There are only \(K+1 \le 11\) possible altitudes

Letting the state be “(base \(v\), altitude \(x\))”, the number of states is \(N(K+1) \le 11000\). There are three types of edges:

  • Increase altitude: \((v,x)\to(v,x+1)\) with cost \(P_v\) (only when \(x<V\))
  • Decrease altitude: \((v,x)\to(v,x-1)\) with cost \(Q_v\) (only when \(x>U\))
  • Flight route \(j\): for each \(x\) satisfying \(L_j\le x\le R_j\), \((A_j,x)\to(B_j,x)\) with cost \(C_j+D_j|x-H_j|\)

Since all costs are non-negative, the shortest path can be found using Dijkstra’s algorithm.

Insight 3: There are at most 66 pairs of \((U,V)\)

Since the range must include \(S\) and \(T\), we have \(U \le \min(S,T)\) and \(V \ge \max(S,T)\). Since \(K\le 10\), the number of pairs is at most \(11\times 12/2=66\). Trying all possible pairs easily runs within the time limit.

Comparison with a Naive Approach

One might consider including “the minimum and maximum altitude visited so far” in the state. However, in that case, the number of states increases to around \(N\cdot K^3\), and the implementation becomes complicated. By iterating over all ranges on the outside, the inner problem is simply Dijkstra’s algorithm.

Note that it is not an issue if the actual altitude range used is narrower than the assumed \([U,V]\). Since we iterate over all ranges, the case where exactly that narrower range is set is also tested, ensuring that the correct minimum is found.

Pruning

We test \((U,V)\) in ascending order of width \(V-U\). - If \(G(V-U)\) is already greater than or equal to the current best answer, any pair with a larger width cannot improve the answer, so we can terminate early. - During Dijkstra’s algorithm, if the popped distance is greater than or equal to “current best \(- G(V-U)\)”, continuing the search is futile, so we can terminate early.

Algorithm

  1. For each flight route \(j\) and each altitude \(x\in[L_j,R_j]\), precompute the edge \((A_j,x)\to(B_j,x)\) (with cost \(C_j+D_j|x-H_j|\)) in the state graph.
  2. Enumerate all pairs of \(U\in[0,\min(S,T)]\) and \(V\in[\max(S,T),K]\), and sort them in ascending order of width.
  3. For each pair, perform the following:
    • If \(G(V-U)\ge\) best value, terminate the search.
    • Run Dijkstra’s algorithm from state \((1,S)\). Altitude-change edges are restricted to within \([U,V]\). Since flight route edges do not change the altitude, if the starting altitude is within the range, the destination altitude is also within the range.
    • If \((N,T)\) is reached, update the best value with \(G(V-U)+\text{dist}\).
  4. If the best value was not updated, output NA; otherwise, output the best value.

Complexity

Let the number of states be \(V'=N(K+1)\) and the number of edges be \(E'=2N(K+1)+M(K+1)\). - Time complexity: \(O\!\left(K^2\,(N+M)K\log(NK)\right)\) (running Dijkstra once for each of the \(K^2\) ranges) - Space complexity: \(O((N+M)K)\)

Implementation Notes

  • State \((v,x)\) is represented by the integer \(v\cdot(K+1)+x\). This allows altitude changes to be written simply as \(\pm1\) to the state index.

  • Instead of pushing a tuple (distance, state) into the heap, push a single integer distance * NS + state (NS is the number of states). When popped, the distance and state are recovered using the quotient and remainder of division by NS. In Python, comparing integers is faster than comparing tuples, resulting in a constant-factor speedup.

  • The adjacency list is stored as a flat list [destination, cost, destination, cost, ...] to reduce object creation overhead.

  • Dijkstra’s algorithm can be terminated as soon as the target state \((N,T)\) is popped from the heap.

  • Examining ranges in ascending order of width and pruning with base >= best for the outer loop and d >= limit for the inner Dijkstra reduces unnecessary searches.

    Source Code

import sys, heapq
def main():
    data = sys.stdin.buffer.read().split()
    idx = 0
    N = int(data[0]); M = int(data[1]); K = int(data[2]); G = int(data[3])
    S = int(data[4]); T = int(data[5])
    idx = 6
    P = [0]*N; Q = [0]*N
    for i in range(N):
        P[i] = int(data[idx]); Q[i] = int(data[idx+1]); idx += 2
    W = K + 1
    NS = N * W
    adj = [[] for _ in range(NS)]
    for _ in range(M):
        a = int(data[idx])-1; b = int(data[idx+1])-1
        L = int(data[idx+2]); R = int(data[idx+3])
        C = int(data[idx+4]); H = int(data[idx+5]); D = int(data[idx+6])
        idx += 7
        ba = a*W; bb = b*W
        for x in range(L, R+1):
            lst = adj[ba+x]
            lst.append(bb+x)
            lst.append(C + D*abs(x-H))
    INF = float('inf')
    best = INF
    lo = min(S, T); hi = max(S, T)
    pairs = []
    for U in range(0, lo+1):
        for V in range(hi, K+1):
            pairs.append((V-U, U, V))
    pairs.sort()
    target = (N-1)*W + T
    start = S
    heappush = heapq.heappush; heappop = heapq.heappop
    BIG = INF
    for width, U, V in pairs:
        base = G * width
        if base >= best:
            break
        limit = best - base
        dist = [BIG]*NS
        dist[start] = 0
        h = [start]
        while h:
            item = heappop(h)
            d = item // NS
            s = item - d*NS
            if d != dist[s]:
                continue
            if d >= limit:
                break
            if s == target:
                break
            v = s // W
            x = s - v*W
            if x < V:
                nd = d + P[v]
                t = s + 1
                if nd < dist[t]:
                    dist[t] = nd
                    heappush(h, nd*NS + t)
            if x > U:
                nd = d + Q[v]
                t = s - 1
                if nd < dist[t]:
                    dist[t] = nd
                    heappush(h, nd*NS + t)
            lst = adj[s]
            for k in range(0, len(lst), 2):
                t = lst[k]
                nd = d + lst[k+1]
                if nd < dist[t]:
                    dist[t] = nd
                    heappush(h, nd*NS + t)
        dt = dist[target]
        if dt < BIG:
            tot = base + dt
            if tot < best:
                best = tot
    if best == INF:
        print("NA")
    else:
        print(best)
main()

This editorial was generated by claude5.5opus-high.

posted:
last update: