_Never_

I walk slowly,but I will never stop.

导航

Bellman_Ford

//-----------Bellman_Ford------------
struct Edge
{
int v,u;
int weight;
}edge[M];
int dist[N];

bool Bellman_Ford(int n,int m,int s)
{
int i,j;
bool flag;
for(i=1;i<=n;i++)
dist[i]
=MAXVAL;
dist[s]
=0;

for(i=1;i<n;i++)
{
flag
=false;
for(j=0;j<m;j++)
{
if(dist[edge[j].v]+edge[j].weight<dist[edge[j].u])
dist[edge[j].u]
=dist[edge[j].v]+edge[j].weight,flag=true;
}
if(flag==false) return true;
}
///显然写的是单向的!
for(j=0;j<m;j++)
{
if(dist[edge[j].v]+edge[j].weight<dist[edge[j].u])
return false;
}

return true;
}

写的不对或不好的地方,请大虾多多指教!    ——Never

posted on 2011-09-14 14:27  _Never_  阅读(134)  评论(0编辑  收藏  举报