# 单源最短路径（bellman-ford 、dijkstra）

Bellman-ford算法：

``````template< typename VertexType, typename CostType >
bool Graph<VertexType, CostType>::bellman_ford( int source, vector<int> &distance )//序号为source的节点作为源点.
{
initialize_single_source( source, distance );
for( int i = 1; i < vex_num; ++i )//最远的点经过vertex_num-1条边也到了.
{
for( vector< Vertex<VertexType, CostType> >::iterator
iter = vertex_table.begin();
iter != vertex_table.end();
++iter )
{
int begin = iter->serial_number;
while( edge != NULL )
{
int end = edge->end_vertex;
relax( begin, end, distance );
}
}
}
for( vector< Vertex<VertexType, CostType> >::iterator
iter = vertex_table.begin();
iter != vertex_table.end();
++iter )
{
int begin = iter->serial_number;
while( edge != NULL )
{
int end = edge->end_vertex;
if( distance[end-1] > distance[begin-1]	+ edge->cost )
return false;
}
}
return true;
}
``````

Dijkstra算法：

原文作者：Bellman - ford算法
原文地址: https://blog.csdn.net/march_on/article/details/6926086
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。