link
Firstly, we can transform this question to a graph, let all points 0 , n − 1 0,n-1 0,n−1 and a virtual point n n n
We got a directed positive-weighted graph.
If we don’t consider the Rank-Constrain, the answer is the minimal path from 0 → n 0 \to n 0→n.
The answer path non contains any loop, but the graph will contains loop.
If you use DFS + Remembered-DP, that is erroneous, because this graph is not a DAG.
e.g.
0------->1---->2
| ^ |
| | |
| | |
-------->3<-----
DFS(0), if it visit 1
firstly, then go to 2
, and when we arrive at 3
, it will also DFS(1), but DFS(1) is not finished, so D i s t a n c e [ 1 ] Distance[1] Distance[1] is wrong, then D i s t a n c e [ 3 ] Distance[3] Distance[3] is also wrong.
Consequently, when 0 → 3 0 \to 3 0→3, 3 3 3 returns a wrong Distance.
(suppose the answer is 0 → 3 → 1 → 2 → n 0 \to 3 \to 1 \to 2 \to n 0→3→1→2→n)
The right approach is just Dijkstra.
Let’s consider Rank-Limitation, it’s vital to comprehend the meaning.
Although you just have one stuff 0 0 0, but you may involve many staffs in the whole process.
More precisely, for a path 0 → 1 → 2 → n 0 \to 1 \to 2 \to n 0→1→2→n, you involved 0 , 1 , 2 0, 1, 2 0,1,2.
The Rank-Limitation shows that the rank a , b a, b a,b of any two staffs in the path, should satisfying ∣ a − b ∣ ≤ M |a - b| \leq M ∣a−b∣≤M
So, we scan all the possible interval [ 1 , 1 + M ] , [ 2 , 2 + M ] , [ 3 , 3 + M ] , . . . [1, 1+M], [2, 2+M], [3, 3+M], ... [1,1+M],[2,2+M],[3,3+M],... denotes that a point is valid in the graph if the rank of the point locates in the interval.
There are some crucial points: