I\'ve learnt a dynamic programming algorithm to find the \"cheapest\" path from A to B. Each sub path has an associated cost.
Each corner is calculated using
I believe what you are referring to is the all-pair shortest paths problem. Here is a good description of the algorithm. And here is a google book reference that goes through how to solve it.