Shortest Path algorithm 1--floyed and Dijkstra algorithm.
To find the shortest path of a point to another point in the graph, there is no doubt that the floyed algorithm is the simplest, and is a multi-source shortest path, but the time complexity is very high to reach O (n^3).
The principle is to constantly traverse all points on one side, and treat them as intermediate points, updating the entire graph every time.
Floyed Code:
1#include <cstdio>2#include <iostream>3 #defineN 42004 using namespacestd;5 intN,m,p,q,a,b,c,dis[n][n];6 intMain () {7scanf"%d%d%d%d",&n,&m,&p,&q);8 for(intI=1; i<=n;++i)9 for(intj=1; j<=n;++j)Tendis[i][j]=4200000; One for(intI=1; i<=m;++i) { Ascanf"%d%d%d",&a,&b,&c); -dis[a][b]=C; -dis[b][a]=C; the } - for(intk=1; k<=n;++k) - for(intI=1; i<=n;++i) - for(intj=1; j<=n;++j) +Dis[i][j]=min (dis[i][j],dis[i][k]+dis[k][j]); -printf"%d", Dis[p][q]); + return 0; A}
View Code
Shortest Path algorithm