we have to find min cost path in a mtrix from top left to bottom right.
Move allowed -> Right,left,Bottom,Up.
I know suitable approach will be using Dijkstra But can we do this using Dp because here we can move in all 4 direction ,If yes then How?
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3741 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3489 |
7 | Radewoosh | 3483 |
8 | Kevin114514 | 3442 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
2 | atcoder_official | 162 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | nor | 150 |
we have to find min cost path in a mtrix from top left to bottom right.
Move allowed -> Right,left,Bottom,Up.
I know suitable approach will be using Dijkstra But can we do this using Dp because here we can move in all 4 direction ,If yes then How?
Name |
---|
is cost value always non negative?
If you can move in all four directions, you have a graph with cycles and then you can't use dp.
If the cost values are non negative is it still true that we can't use dp.
Yes. Because what would dp transitions look like? There can be no cycles among them.
That's how I implemented it can it be more efficient. https://ideone.com/tvKC9r
Can anyone tell me what will be the time complexity of my solution ? I am using dijkstra on a matrix (in above soln) ?
Ffs, make a loop over adjacent cells. Don't repeat the same piece of code 4 times.
What is the number of states (vertices of a graph)? What is the number of transitions (edges)? And finally, what is the complexity of Dijkstra?