Dynamic Programming, but faster:
weight of a shortest path from i to j with intermediate vertices in
Then
Pseudo-code: