An interesting problem with a minimum price

There are n bus stops, and we know the fee between the i-th and j-th stops. This is a one-way road. What is the minimum price of the route from the 1st to the n-th stop, taking into account all possible connections? The consumption of time and memory should be the least possible.

ps For example, there are 4 stops. We have such a price table:

. 3 $ 5 $ 7 $
. . 1 $ 3 $
. . . 1 $

go from 1st to 4th, non-stop we pay $ 7. if we change routes at the second stop, we pay $ 3 + $ 1 = $ 4 to get to the third stop, but we pay $ 2 more if we go to the end, so in general it will cost $ 6, but again however, if we change routes at the 3rd stop, we will pay 4 + 1 = $ 5.

+3
source share
2 answers

d[i][j] - , l[k] - k n.

l[n] = 0

l[k] = min(d[k][i] + l[i], i=k+1..n)

O (n ^ 2). (, , .)

+5

. , , - , .

+3

Source: https://habr.com/ru/post/1788133/


All Articles