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]\) for the drone. For each range, we find the shortest time using Dijkstra’s algorithm on a graph where each vertex represents a “(base, altitude)” state. Finally, we output the minimum value after adding the adjustment time \(G \times (V-U)\).

Analysis

Representing the State as “(Base, Altitude)”

Whether a flight route can be used and its travel time depend on the altitude at that moment. Therefore, we represent the state as a pair: (current base \(i\), current altitude \(x\)). Then, each operation can be represented as a weighted directed edge as follows:

  • Increase altitude at base \(i\): \((i, x) \to (i, x+1)\) with cost \(P_i\)
  • Decrease altitude at base \(i\): \((i, x) \to (i, x-1)\) with cost \(Q_i\)
  • Traverse flight route \(j\): \((A_j, x) \to (B_j, x)\) with cost \(C_j + D_j|x - H_j|\) (only when \(L_j \le x \le R_j\))

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

The Challenge: Cost of the Altitude Range

The total travel time also includes \(G \times (V - U)\). This quantity depends on the difference between the maximum and minimum altitudes used along the entire path. Because it cannot be expressed simply by summing up edge costs, it cannot be directly incorporated into standard Dijkstra’s algorithm as is.

A naive approach would be to include the “minimum and maximum altitudes visited so far” in the state. However, this increases the number of states and makes implementation cumbersome.

Solution: Brute-Forcing the Altitude Range

Notice here that \(K \le 10\) is very small.

  • \(U\) must be chosen such that \(0 \le U \le \min(S,T)\)
  • \(V\) must be chosen such that \(\max(S,T) \le V \le K\)

The number of pairs \((U, V)\) is at most around \(6 \times 6 = 36\) (when \(S = T = 5\) and \(K=10\)).

Therefore, we can fix \((U, V)\) and run Dijkstra’s algorithm using only states whose altitudes stay within \([U, V]\). Once \((U, V)\) is fixed, the adjustment time \(G(V-U)\) becomes a constant. The answer can be obtained using the following formula:

\[\min_{U, V} \Big( G(V-U) + \text{shortest time within range } [U,V] \Big)\]

The altitude range actually utilized by a path might be narrower than the chosen \([U, V]\), but such a path will be evaluated with an even smaller cost when testing that narrower range. Thus, the overall minimum is correctly computed.

Example

Suppose \(K=3\), \(S=1\), and \(T=2\). In this case, we test the \(4\) combinations where \(U \in \{0,1\}\) and \(V \in \{2,3\}\). For example, for \([U,V]=[1,2]\), we run Dijkstra’s algorithm on the graph containing only states with altitudes \(1\) and \(2\), and add \(G \times 1\) to the resulting shortest time.

Algorithm

  1. Iterate over \(U = 0, \dots, \min(S,T)\) and \(V = \max(S,T), \dots, K\) using a nested loop.
  2. For each \((U,V)\), perform the following:
    • Let base \(= G(V-U)\). If this is already greater than or equal to the current best answer best, skip this pair.
    • Run Dijkstra’s algorithm starting from \((1, S)\) on a graph with \(N \times (V-U+1)\) vertices.
    • There are three types of edges: “increase altitude”, “decrease altitude” (only within the range \([U,V]\)), and “flight route” (only when \(L_j \le x \le R_j\)).
    • If \((N, T)\) is reachable, update best with base + dist.
  3. If best remains infinity, output NA; otherwise, output best.

The following two prunings are applied: - Terminate Dijkstra’s algorithm as soon as the target state \((N,T)\) is extracted from the heap. - Terminate early once d + base >= best, as continuing the search will not improve the answer.

Complexity

Let the number of possible altitudes be \(W = K+1\). The number of pairs \((U,V)\) is \(O(K^2)\). For each run of Dijkstra’s algorithm, the number of vertices is \(O(NW)\) and the number of edges is \(O((N+M)W)\).

  • Time Complexity: \(O\big(K^2 \cdot (N+M)K \log(NK)\big)\)
    • In practice, the number of pairs is at most around \(36\), and the number of edges per run is approximately \(8 \times 10^4\), which is sufficiently fast.
  • Space Complexity: \(O((N+M) + NK)\)

Implementation Details

  • State Indexing: We map \((i, x)\) to a 1D index using i * w + (x - U) (where \(w = V-U+1\)). This allows altitude increment and decrement operations to be represented simply as \(\pm 1\) on the index.

  • Overflow Prevention: The cost of a single flight route can be up to \(10^6 + 10^6 \times 10\), and the total cost for the entire path can be much larger. Therefore, use long long.

  • Skipping Outdated Entries in Dijkstra’s Algorithm: When d != dist[id], the entry is outdated and should be skipped.

  • Unreachability Check: In cases such as \(M = 0\), \((N,T)\) might be unreachable. If best remains INF, output NA.

    Source Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
    int N,M,K; ll G;
    scanf("%d %d %d %lld",&N,&M,&K,&G);
    int S,T; scanf("%d %d",&S,&T);
    vector<ll> P(N),Q(N);
    for(int i=0;i<N;i++) scanf("%lld %lld",&P[i],&Q[i]);
    struct E{int to,L,R,H; ll C,D;};
    vector<vector<E>> adj(N);
    for(int j=0;j<M;j++){
        int a,b,l,r,h; ll c,d;
        scanf("%d %d %d %d %lld %d %lld",&a,&b,&l,&r,&c,&h,&d);
        a--;b--;
        adj[a].push_back({b,l,r,h,c,d});
    }
    const ll INF = LLONG_MAX/4;
    ll best = INF;
    int lo = min(S,T), hi = max(S,T);
    for(int U=0;U<=lo;U++){
        for(int V=hi;V<=K;V++){
            ll base = G*(V-U);
            if(base>=best) continue;
            int w = V-U+1;
            vector<ll> dist((size_t)N*w, INF);
            priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>> pq;
            int st = 0*w + (S-U);
            dist[st]=0; pq.push({0,st});
            int target = (N-1)*w + (T-U);
            while(!pq.empty()){
                auto [d,id]=pq.top(); pq.pop();
                if(d!=dist[id]) continue;
                if(id==target) break;
                if(d+base>=best) break;
                int i = id / w; int x = id % w + U;
                // up
                if(x+1<=V){
                    int nid=id+1; ll nd=d+P[i];
                    if(nd<dist[nid]){dist[nid]=nd;pq.push({nd,nid});}
                }
                if(x-1>=U){
                    int nid=id-1; ll nd=d+Q[i];
                    if(nd<dist[nid]){dist[nid]=nd;pq.push({nd,nid});}
                }
                for(auto &e:adj[i]){
                    if(x<e.L||x>e.R) continue;
                    ll c = e.C + e.D*(ll)abs(x-e.H);
                    int nid = e.to*w + (x-U);
                    ll nd=d+c;
                    if(nd<dist[nid]){dist[nid]=nd;pq.push({nd,nid});}
                }
            }
            if(dist[target]<INF){
                best=min(best, base+dist[target]);
            }
        }
    }
    if(best>=INF) printf("NA\n");
    else printf("%lld\n",best);
}

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

posted:
last update: