图_单源最短路径_ Dijkstra算法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lvxiangyu11/article/details/79782979
原理请参考算导
#define DEBUG



#include<iostream>
#include<queue>
#include<climits>
#include<vector>
#include<set>

/*
This programme is realize Dijkstra Algorithm
Editor:Xiangyu Lv
email:[email protected]
frist-edit-time:2018年4月1日17:09:47
ALL RIGHTS RESERVED!
*/

using namespace std;

typedef int Vertex;
typedef int Weight;

#define originLoc 0


class adjacentList {
public:
	explicit adjacentList() {
		List.resize(0);
	}

	void insert(Vertex v0, Vertex v1, Weight w) {
		size_t v0Loc = find(v0);
		if (v0Loc == -1)
			v0Loc = newNode(v0);
		size_t v1Loc = find(v1);
		if (v1Loc == -1)
			v1Loc = newNode(v1);

		EdgeNode * newEN = new EdgeNode;
		newEN->bratherNode = List[v0Loc].adjacentEdge;
		newEN->Loc = v1Loc;
		newEN->weight = w;
		List[v0Loc].adjacentEdge = newEN;
	}

	void Dijkstra() {
		//waitSet.clear();
		//while (!Locpq.empty())
		//	Locpq.pop();
		//for (size_t i = 0; i < List.size(); i++)//Q初始化
		//	waitSet.insert(List[i].Loc);

		for (size_t i = 0; i < List.size(); i++) {//init
			List[i].front = 0;
			List[i].w = 100000;
			List[i].know = false;
			//	Locpq.push
		}

		List[originLoc].w = 0;


		while (1) {
			int tempLoc;

			//size_t tempLoc = Locpq.top();
			//Locpq.pop();
			//waitSet.erase(tempLoc);
			while ((tempLoc=dijkstra_find())!=-1) {
				EdgeNode * tempENP = List[tempLoc].adjacentEdge;
				List[tempLoc].know = true;
				
				while (tempENP != nullptr) {
					if (List[tempENP->Loc].w > List[tempLoc].w + tempENP->weight) {
#ifdef DEBUG
						cout << "调整:" << List[tempENP->Loc].v << " Weight Front: " << List[tempENP->Loc].w << "  " << List[tempENP->Loc].front;
#endif // DEBUG
						List[tempENP->Loc].w = List[tempLoc].w + tempENP->weight;
						List[tempENP->Loc].front= List[tempLoc].Loc;
#ifdef DEBUG
						cout<<" -> "<< List[tempENP->Loc].w << "  " << List[tempENP->Loc].front<<endl;
#endif // DEBUG
					}
					tempENP = tempENP->bratherNode;
				}
			}


		}

	}

private:
	struct EdgeNode
	{
		size_t Loc;
		Weight weight;
		EdgeNode * bratherNode;
	};
	struct ListNode
	{
		size_t Loc;
		Vertex v;
		Weight w;
		size_t front;
		EdgeNode * adjacentEdge;
		bool know;
	};
	//priority_queue<size_t> Locpq;

	vector<ListNode> List;
	//set<size_t> waitSet;
	int dijkstra_find() {
		int minLoc=-1;
		for(size_t i=0;i<List.size();i++)
			if(!List[i].know)
				if (minLoc == -1 || List[i].w < List[minLoc].w) {
					minLoc = i;
				}
		return minLoc;
	}

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

	int newNode(Vertex v) {
		ListNode newLN;
		newLN.adjacentEdge = nullptr;
		newLN.Loc = List.size();
		newLN.v = v;
		List.push_back(newLN);
		return newLN.Loc;
	}

};


int main() {
	adjacentList MyAj;

	size_t i;
	cin >> i;
	for (size_t j = 0; j < i; j++) {
		Vertex v0, v1;
		Weight w;
		cin >> v0 >> v1 >> w;
		MyAj.insert(v0, v1, w);
	}
	MyAj.Dijkstra();

	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:
调整:2 Weight Front: 100000  0 -> 5  0
调整:1 Weight Front: 100000  0 -> 1  0
调整:4 Weight Front: 100000  0 -> 6  1
调整:3 Weight Front: 100000  0 -> 8  1
调整:2 Weight Front: 5  0 -> 4  1
调整:5 Weight Front: 100000  0 -> 11  2
调整:4 Weight Front: 6  1 -> 5  2
调整:7 Weight Front: 100000  0 -> 14  4
调整:6 Weight Front: 100000  0 -> 11  4
调整:5 Weight Front: 11  2 -> 8  4
调整:3 Weight Front: 8  1 -> 7  4
调整:6 Weight Front: 11  4 -> 10  3
调整:7 Weight Front: 14  4 -> 13  5
调整:8 Weight Front: 100000  0 -> 17  6
调整:7 Weight Front: 13  5 -> 12  6
调整:8 Weight Front: 17  6 -> 16  7
*/

猜你喜欢

转载自blog.csdn.net/lvxiangyu11/article/details/79782979