非负权值的单源最短路之 dijkstra
Tanky Woo之dijkstra: http://www.wutianqi.com/?p=1890
dijkstra-------我认为这个算法的核心思想是:最短路径长度递增。其实就是一个贪心算法。
怎么理解呢?
最短路的最优子结构:假如有一条最短路径已经存在了,那么其中任意两点的路径都将是最短的,否则假设是不成立了。
算法实现过程:
- 已当前点 pos 更新,dis[ i ]的值(即 点 i 到源点的距离)
- 找出dis[ i ] 最小的 i 点,作为当前 pos 点,并且加入集合 S ,
- 重复 1 ,2步骤,N次,因为每次都会找出一个点,一共只有N个点;
以poj 2457为例:
Part Acquisition
Description
The cows have been sent on a mission through space to acquire a new milking machine for their barn. They are flying through a cluster of stars containing N (1 <= N <= 50,000) planets, each with a trading post.
The cow
The cow