这个算法实现比dijkstra+heap简单,但是时间效率低一些 (O(VE))。可以判断是否有负权的环。
算法的基础是单源最短路问题中的path-relaxation property:设v_0到v_k的最短路为p={v_0, .... ,v_k},若p被relax的顺序是(v_0, v_1),...,(v_k-1,v_k),则最后得到的v_0到p上各点的距离就是最短距离。
#include <iostream>
#define MAXN 200
#define INF 0xfffffff
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
using namespace std;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int list[MAXN][MAXN][2]; // 存储图,list[i][j][0]表节点编号,list[i][j][1]表节点i到list[i][j][0]的距离
int deg[MAXN]; // 各节点的出度
int n; // 节点数
int trace[MAXN]; // 存储最短路
int d[MAXN]; // 算法结束时d[i]存源到节点i的最短距离
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void initialize_single_source()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int i;
for(i=1;i<=n;i++) d[i]=INF;
d[1]=0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void relax(int u,int v)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if(d[list[u][v][0]]>d[u]+list[u][v][1])
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
trace[list[u][v][0]]=u;
d[list[u][v][0]]=d[u]+list[u][v][1];
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int bellman_ford()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{// 若存在负环返回-1;否则返回1
int i,j,k;
initialize_single_source();
for(i=0;i<n-1;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{// 对所有边relax n-1次
for(j=1;j<=n;j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{// 遍历所有边
for(k=0;k<deg[j];k++) relax(j,k);
}
}
// 检查是否有负权的环
for(i=1;i<=n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(j=0;j<deg[i];j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(d[list[i][j][0]]>d[i]+list[i][j][1]) return -1;
}
}
//for(i=1;i<=n;i++) printf("%d distance:%d\n",trace[i],d[i]);
return 1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void readdata()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
scanf("%d",°[i]);
for(j=0;j<deg[i];j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
scanf("%d%d",&list[i][j][0],&list[i][j][1]);
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
freopen("test.txt","r",stdin);
readdata();
printf("%d",bellman_ford());
return 1;
}