图算法总结---Bellman_Ford算法

//
// Created by liyuanshuo on 2017/3/15.
//
#include <queue>
#include <cstring>
#include <afxres.h>
#include <vector>

const int inf = 0x7fffffff;
const int Maxnn = 10010;
int d[Maxnn];//p[Maxnn];
int inq[Maxnn];
int ub[Maxnn], vb[Maxnn], wb[Maxnn];
int cnt[Maxnn];
//
//
//n---numbers of nodes
//m---numbers of sides
void SimpleThought( int n, int m )
{
	for (int i = 0; i < n ; ++i)
	{
		d[i] = inf;
	}
	d[0] = 0;
	for (int j = 0; j < n-1; ++j)
	{
		for (int i = 0; i < m; ++i)
		{
			int x = ub[i];
			int y = vb[i];
			if( d[x] < inf )
				d[y] = std::min(d[y], d[x]+ wb[i]);
		}
	}
}

struct Edage
{
	int from;
	int to;
	int dist;
	Edage( int u, int v, int d ) : from(u), to(v), dist(d) { }
};

bool bellman_ford( int n, int s)
{
	int p[Maxnn];
	std::queue<int> Q;
	std::vector<Edage> edges;
	std::vector<int> G[Maxnn];
	memset ( inq, 0, sizeof (inq));
	memset ( cnt, 0, sizeof (cnt));
	for(int i = 0; i < n; i++ )
	{
		d[i] = inf;
	}
	d[s] = 0;
	inq[s] = true;
	Q.push (s);
	
	while( !Q.empty () )
	{
		int u = Q.front ();
		Q.pop ();
		inq[u] = false;
		for (int i = 0; i < G[u].size(); ++i)
		{
			Edage& e = edges[G[u][i]];
			if( d[u] < inf && d[e.to] > d[u] + e.dist);
			{
				d[e.to] = d[u] + e.dist;
				p[e.to] = G[u][i];
				if ( !inq[e.to] )
				{
					Q.push (e.to);
					inq[e.to] = true;
					if( ++cnt[e.to] > n )
						return false;
				}
			}
		}
	}
	return true;
}

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