Shortest path (miscellany)

The shortest path is a common use in trees. My study started with two blogs. The links are as follows:
Floyd algorithm: https://blog.csdn.net/shaonianbz/article/details/79709408
Synthesis: https://blog.csdn.net/createprogram/article/details/86710519
OK, here is my own understanding.

Floyd algorithm

Floyd's simplest and shortest path is the output of five lines of code. It can be understood that it is a violent simulation of the shortest path process. It has been introduced in the first blog above, so it's not much to say, just go to the code.

//Floyd algorithm
void floyd(int n){//Floyd algorithm
	for (int k=1 ; k <=n ; k++)//Transit point
		for (int i=1 ; i <= n ; i++)//starting point
			for (int j=1 ; j <=n ; j++)//Destination
				if (arr[i][j] > arr[i][k] + arr[k][j])
					arr[i][j] = arr[i][k] + arr[k][j];
}

Dijkstra algorithm

Simply speaking, Dijkstra algorithm belongs to greed. In addition, consider the difference between Dijkstra and Floyd. The difference lies in the starting point. Finally, consider the relationship between Dijkstra and the minimum spanning tree. In fact, Dijkstra is the minimum spanning tree of many times. It only modifies some rules (if statement and the last arr ay update), so the code is as follows:

//Dijkstra algorithm
void Dijkstra(int n) {//Dijkstra algorithm
	for (int  A = 1; A <=n ; A++) {//Start multiple times
		for (int i = 1; i <= n; i++) {//Initialize dis value
			dis[i] = arr[A][i];
			brr[i] = 0;
		}
		for (int i = 1; i <= n; i++) {//Traverse all endpoints
			int S = 99, k;
			for (int j = 1; j <= n; j++) {//Find the minimum value of dis and mark
				if (brr[j] == 0 && dis[j] < S) {
					S = dis[j];
					k = j;
				}
			}
			brr[k] = 1;//Mark this point has passed
			if (S == 99) break;//It's over when there's no way to go
			for (int i = 1; i <= n; i++) {//Update dis array
				if (brr[i] == 0 && dis[i] > dis[k] + arr[k][i])
					dis[i] = dis[k] + arr[k][i];
			}
		}
		for (int i = 1; i <= n; i++)//Update to arr array
			arr[A][i] = dis[i];
	}
}

Shortest overall formwork

#include<iostream>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<queue>
#include<iomanip>
#include<map>
#include<set>
#include<functional>
using namespace std;
int arr[100][100],dis[100],brr[100];
void again(int n) {//Reset arr array
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++)
			arr[i][j] = 99;
		arr[i][i] = 0;
	}
}

void show(int n) {//Output arr array
	for (int i = 1; i <=n; i++) {
		for (int j = 1; j <=n; j++)
					cout << arr[i][j] << ' ';
		cout << endl;
	}
}

void floyd(int n){//Floyd algorithm
	for (int k=1 ; k <=n ; k++)//Transit point
		for (int i=1 ; i <= n ; i++)//starting point
			for (int j=1 ; j <=n ; j++)//Destination
				if (arr[i][j] > arr[i][k] + arr[k][j])
					arr[i][j] = arr[i][k] + arr[k][j];
}

void Dijkstra(int n) {//Dijkstra algorithm
	for (int  A = 1; A <=n ; A++) {//Start multiple times
		for (int i = 1; i <= n; i++) {//Initialize dis value
			dis[i] = arr[A][i];
			brr[i] = 0;
		}
		for (int i = 1; i <= n; i++) {//Traverse all endpoints
			int S = 99, k;
			for (int j = 1; j <= n; j++) {//Find the minimum value of dis and mark
				if (brr[j] == 0 && dis[j] < S) {
					S = dis[j];
					k = j;
				}
			}
			brr[k] = 1;//Mark this point has passed
			if (S == 99) break;//It's over when there's no way to go
			for (int i = 1; i <= n; i++) {//Update dis array
				if (brr[i] == 0 && dis[i] > dis[k] + arr[k][i])
					dis[i] = dis[k] + arr[k][i];
			}
		}
		for (int i = 1; i <= n; i++)//Update to arr array
			arr[A][i] = dis[i];
	}
}
int main()
{
	int a, b, c,n,m;
	cin >> n >> m;
	again(n);
	for (int i=0; i < m; i++) {
		cin >> a >> b >> c;
		arr[a][b] = c;
	}
	Dijkstra(n);
	show(n);
	floyd(n);
	show(n);
	return 0;
}

Last

The shortest path is not difficult. You can understand it by drawing more pictures in combination with the two blogs provided above. Just think more.

Posted by pdunn on Tue, 15 Oct 2019 12:37:59 -0700