单源最短路径Bellman Ford算法c++11实现

导读自己参考《算法导论》(第三版)p379

#define DEBUG


#include<iostream>
#include<vector>
#include<climits>
/*
This programme is designed to show Bellman Ford minway;
Editor:Xiangyu Lv(Nangking)
E-mail:lvxiangyu11@gmail.com
Frist Edit time:2018年4月1日14:03:27
ALL RIGHTS RESERVED!
*/
using namespace std;

typedef int Vertex;
typedef int Weight;

class AdjacencyList {
public:
	explicit AdjacencyList(){
		List.resize(0);
		Namestroy.resize(0);
		edgeNum = 0;
		EdgeList.resize(0);
	}
	void insert(Vertex v0, Vertex v1, Weight w) {
		size_t v0Loc = find(v0);
		if (v0Loc == -1)
			v0Loc = newVertex(v0);
		size_t v1Loc = find(v1);
		if (v1Loc == -1)
			v1Loc = newVertex(v1);

		ListEdgeNode * newEN=new ListEdgeNode;
		newEN->bratherNode =List[v0Loc].adjacencyNode;
		newEN->Loc = v1Loc;
		newEN->vertex = v1;
		newEN->weight = w;
		List[v0Loc].adjacencyNode = newEN;
		edgeNum++;

		EdgeNode newEdge;
		newEdge.v0Loc = v0Loc;
		newEdge.v1Loc = v1Loc;
		newEdge.w = w;
		EdgeList.push_back(newEdge);
	}

	bool BellmanFord() {
		for (size_t i = 0; i < List.size(); i++) {
			List[i].weight = INT32_MAX;
			List[i].frountLoc= 0;
		}
		List[0].weight = 0;
		for (size_t i = 0; i < List.size(); i++) {
			for (size_t j = 0; j < edgeNum; j++) {
				if (List[EdgeList[j].v1Loc].weight > List[EdgeList[j].v0Loc].weight + EdgeList[j].w) {
#ifdef DEBUG
					ListTruck _debug_temp_list_truck = List[EdgeList[j].v1Loc];
#endif // DEBUG
					List[EdgeList[j].v1Loc].weight = List[EdgeList[j].v0Loc].weight + EdgeList[j].w;
					List[EdgeList[j].v1Loc].frountLoc = EdgeList[j].v0Loc;
#ifdef DEBUG
					cout << "调整 " << _debug_temp_list_truck.vertex << ":Weight: " << _debug_temp_list_truck.weight << " -> "
						<< List[EdgeList[j].v1Loc].weight << " From: "
						<< _debug_temp_list_truck.frountLoc << " -> " << List[EdgeList[j].v1Loc].frountLoc << endl;
#endif // DEBUG
				}
			}
		}
		for (size_t i = 0; i < List.size(); i++) {
			for (size_t j = 0; j < edgeNum; j++) 
				if (List[EdgeList[j].v1Loc].weight > List[EdgeList[j].v0Loc].weight + EdgeList[j].w) {
#ifdef DEBUG
					cout << "出现负环路:" << List[EdgeList[j].v0Loc].vertex << "->" << List[EdgeList[j].v1Loc].vertex << endl;
#endif // DEBUG
#ifndef DEBUG
					return false;

#endif // !DEBUG
				}
		}
		return true;//No Negetive Loop
	}


private:
	struct ListEdgeNode
	{
		Vertex vertex;
		Weight weight;
		ListEdgeNode *bratherNode;
		size_t Loc;
	};
	struct ListTruck{
		Vertex vertex;
		Weight weight;
		size_t Loc;
		ListEdgeNode * adjacencyNode;
		size_t frountLoc;
	};
	struct EdgeNode {
		size_t v0Loc;
		size_t v1Loc;
		Weight w;
	};
	vector<ListTruck> List;
	vector<Vertex> Namestroy;
	vector<EdgeNode> EdgeList;
	size_t edgeNum;
	

	size_t find(Vertex v) {
		for (size_t i=0; i < Namestroy.size(); i++)
			if (Namestroy[i] == v)
				return i;
		return -1;
	}

	size_t newVertex(Vertex v) {
		ListTruck newTruck;
		newTruck.adjacencyNode = nullptr;
		newTruck.Loc = List.size();;
		newTruck.weight = 0;
		newTruck.vertex = v;
		List.push_back(newTruck);
		Namestroy.push_back(v);
		return List.size() - 1;
	}




};


int main() {

	AdjacencyList myAj;
	size_t n;
	cin >> n;
	for (size_t i = 0; i < n; i++) {
		Vertex v0, v1;
		Weight w;
		cin >> v0>>v1>> w;
		myAj.insert(v0, v1, w);
	}

	myAj.BellmanFord();

	system("pause");
	return 0;
}
/*
input:
32
0 1 1
1 0 1
0 2 5
2 0 5
1 2 3
2 1 3
1 3 7
3 1 7
1 4 5
4 1 5
2 4 1
4 2 1
2 5 7
5 2 7
3 4 2
4 3 2
4 5 3
5 4 3
3 6 3
6 3 3
4 6 6
6 4 6
4 7 9
7 4 9
5 7 5
7 5 5
6 7 2
7 6 2
6 8 7
8 6 7
7 8 4
8 7 4

output:

调整 1:Weight: 2147483647 -> 1 From: 0 -> 1
调整 2:Weight: 2147483647 -> 5 From: 0 -> 2
调整 2:Weight: 5 -> 4 From: 2 -> 2
调整 3:Weight: 2147483647 -> 8 From: 0 -> 3
调整 4:Weight: 2147483647 -> 6 From: 0 -> 4
调整 4:Weight: 6 -> 5 From: 4 -> 4
调整 5:Weight: 2147483647 -> 11 From: 0 -> 5
调整 3:Weight: 8 -> 7 From: 3 -> 3
调整 5:Weight: 11 -> 8 From: 5 -> 5
调整 6:Weight: 2147483647 -> 10 From: 0 -> 6
调整 7:Weight: 2147483647 -> 14 From: 0 -> 7
调整 7:Weight: 14 -> 13 From: 7 -> 7
调整 7:Weight: 13 -> 12 From: 7 -> 7
调整 8:Weight: 2147483647 -> 17 From: 0 -> 8
调整 8:Weight: 17 -> 16 From: 8 -> 8

*/

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