I - ドローン配送と高度制御 / Drone Delivery and Altitude Control Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
We iterate over all possible allowed altitude ranges \([U,V]\), and for each range, solve a shortest path problem where the state is defined by (base, altitude). The minimum total time, including the configuration time for the altitude range, is the answer.
Analysis
Include Altitude in the State, Not Just the Base
Even if you arrive at the same base, the available flight routes and travel times depend on your altitude. Therefore, recording only a single shortest time for each base will not yield the correct answer.
Thus, we define the state as:
- \((i,h)\): being at base \(i\) at altitude \(h\)
Since the altitude ranges from \(0\) to \(K\), there are \(N(K+1)\) states.
In a graph where these states are vertices, altitude adjustments and traversing flight routes can be represented by the following edges:
- Ascend: \((i,h)\to(i,h+1)\) with cost \(P_i\)
- Descend: \((i,h)\to(i,h-1)\) with cost \(Q_i\)
- Traverse route \(j\): \((A_j,h)\to(B_j,h)\) with cost \(C_j+D_j|h-H_j|\)
Route edges are created only for altitudes that satisfy \(L_j\le h\le R_j\).
Fix the Altitude Range First
One might consider maintaining the minimum and maximum altitudes reached throughout the entire journey within the state, but managing such states becomes complicated.
Notice that \(K\le 10\) is very small. There are only
\(\dfrac{(K+1)(K+2)}{2}\)
possible candidates for the altitude range \([U,V]\), which is at most \(66\). Therefore, we can simply iterate over all possible ranges.
Furthermore, any range that does not contain both the initial altitude \(S\) and target altitude \(T\) cannot be used. For example, if \(S=2,T=4\), then \([2,4]\) and \([1,5]\) are candidates, but \([3,5]\) is not.
We cannot simply fix the range to \([\min(S,T),\max(S,T)]\). Allowing a wider range might increase the configuration time, but it could enable the use of cheaper routes and reduce the overall total time.
Fixing the Range Allows Dijkstra’s Algorithm
Once \([U,V]\) is fixed, we find the shortest distance from \((1,S)\) to \((N,T)\) using only the states within that range.
Since all edge costs are non-negative, we can use Dijkstra’s algorithm. This allows us to find the minimum time, including journeys that visit bases or flight routes multiple times.
Algorithm
1. Precompute Route Edges
For each route \(j\) and for each \(h=L_j,L_j+1,\ldots,R_j\), add an edge
\((A_j,h)\to(B_j,h)\)
with cost \(C_j+D_j|h-H_j|\).
Edges for altitude adjustments can be generated on the fly during the search as needed.
2. Iterate Over All Altitude Ranges
In the code, the width of the range \(w=V-U\) is checked in ascending order.
- Try \(w=|S-T|,|S-T|+1,\ldots,K\) in order.
- Set \(U=0,1,\ldots,K-w\) and \(V=U+w\).
- Perform the shortest path search below only if both \(S\) and \(T\) are contained in \([U,V]\).
3. Run Dijkstra’s Algorithm for Each Range
Initialize the distance to the starting state \((1,S)\), including the configuration time, as
\(\operatorname{dist}(1,S)=G(V-U)\)
From state \((i,h)\), make the following transitions:
- If \(h<V\), move to \((i,h+1)\) with cost \(P_i\).
- If \(h>U\), move to \((i,h-1)\) with cost \(Q_i\).
- Use the precomputed route edges.
Since altitude does not change along flight routes, if the current altitude is within the range, the destination altitude is also within the range.
When the shortest distance to the destination \((N,T)\) is finalized, consider it as a candidate answer. If it cannot be reached under any range, output NA.
4. Prune the Search Using the Provisional Answer
Let answer be the minimum value found so far. Since edge costs are non-negative, the following pruning can be applied:
- If the configuration time \(Gw\) is greater than or equal to
answer, terminate the search for this width and all larger widths. Increasing the width will not decrease the configuration time. - If the minimum distance popped from the priority queue in Dijkstra’s algorithm is greater than or equal to
answer, terminate that search. - Do not push states to the priority queue if their updated distance is greater than or equal to
answer.
All of these pruning steps skip only states that cannot yield a solution better than the current answer.
Correctness
For a fixed range \([U,V]\), every valid operation corresponds to an edge in the graph, and conversely, every edge corresponds to a valid operation. Additionally, the sum of edge costs equals the total time spent on altitude adjustments and route traversals.
Therefore, the value obtained from Dijkstra’s algorithm is the minimum total time for that range, including the configuration time.
Since we examine all valid altitude ranges and take the minimum, this yields the globally optimal solution.
Complexity
There are \(O(K^2)\) candidate altitude ranges. In each search, the number of vertices handled is \(O(NK)\), and the number of edges (including altitude adjustments) is \(O((N+M)K)\).
In an implementation that pushes to the priority queue on every update (as in the provided code), the complexity is as follows:
- Time Complexity: \(O\!\left((N+M)K^3\log((N+M)K)\right)\)
- Space Complexity: \(O((N+M)K)\)
Although pruning reduces the actual amount of search, the above is an upper bound without considering pruning.
Implementation Notes
Encoding states as integers
By using 0-based indexing for the bases, state \((i,h)\) can be represented as \(i(K+1)+h\). The base and altitude can be recovered using integer division and modulo.Distinguishing between ascent and descent costs
Ascending costs \(P_i\) and descending costs \(Q_i\). Since they may differ, treat them as directed transitions.Using
long longfor distances
Even if the time for a single operation is small, the total time may exceed the range of a 32-bit integer.The reference altitude is not necessarily within the traversable range
Check whether the route is traversable using \(L_j\le h\le R_j\), and compute the cost directly as \(C_j+D_j|h-H_j|\).Ignoring outdated queue elements
If the popped distance differs from the currentdist, skip it. For the destination as well, its shortest distance is finalized once it is popped after this check.Source Code
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Edge {
int to;
int cost;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M, K;
ll G;
cin >> N >> M >> K >> G;
int S, T;
cin >> S >> T;
vector<int> P(N), Q(N);
for (int i = 0; i < N; ++i) {
cin >> P[i] >> Q[i];
}
const int H = K + 1;
const int states = N * H;
vector<vector<Edge>> graph(states);
for (int j = 0; j < M; ++j) {
int A, B, L, R, C, base, D;
cin >> A >> B >> L >> R >> C >> base >> D;
--A;
--B;
for (int h = L; h <= R; ++h) {
graph[A * H + h].push_back({
B * H + h,
C + D * abs(h - base)
});
}
}
const ll INF = numeric_limits<ll>::max() / 4;
ll answer = INF;
const int start = S;
const int goal = (N - 1) * H + T;
vector<ll> dist(states);
for (int width = abs(S - T); width <= K; ++width) {
const ll setup = G * width;
if (setup >= answer) break;
for (int U = 0; U + width <= K; ++U) {
int V = U + width;
if (S < U || S > V || T < U || T > V) continue;
fill(dist.begin(), dist.end(), INF);
priority_queue<pair<ll, int>,
vector<pair<ll, int>>,
greater<pair<ll, int>>> pq;
dist[start] = setup;
pq.emplace(setup, start);
while (!pq.empty()) {
auto [d, v] = pq.top();
pq.pop();
if (d != dist[v]) continue;
if (d >= answer) break;
if (v == goal) {
answer = d;
break;
}
auto relax = [&](int to, int cost) {
ll nd = d + cost;
if (nd < dist[to] && nd < answer) {
dist[to] = nd;
pq.emplace(nd, to);
}
};
int node = v / H;
int altitude = v % H;
if (altitude < V) relax(v + 1, P[node]);
if (altitude > U) relax(v - 1, Q[node]);
for (const Edge& e : graph[v]) {
relax(e.to, e.cost);
}
}
}
}
if (answer == INF) {
cout << "NA\n";
} else {
cout << answer << '\n';
}
return 0;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: