See Shortest path problem - Wikipedia, the free encyclopedia , in particular Dijkstra's algorithm and Floyd-Warshall algorithm. These links will hopefully get you started.