floyed algorithm O (N3) abbreviation floyed (Freud) algorithm, is the simplest shortest path algorithm, can calculate the shortest path between any two points in the graph. The time complexity of the floyed is O (N3), which is suitable for cases where negative edge rights are present. Algorithm analysis & Ideas explained: Three-layer cycle, the first cycle of the middle point K, the second third layer of the beginning of the cycle of the end point I, J, the idea of the algorithm is easy to understand: If the point I points k distance plus point K to J distance is less than the original point I to the distance J, Then use this shorter path length to update the original point I to the distance J. When we initialize, the distance between the unconnected points is set to a large number, it may be considered that the two points are very far apart, if there is a shortest path between the two, it will be updated to the shortest path length. The time complexity of the floyed algorithm is O (N3).
1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 Const intmaxn=1001;7 8 intMAPS[MAXN][MAXN];9 intans;Ten intMain () { Onememset (Maps,999999,sizeof(maps)); A intn,m; -Cin>>n>>m; - intHe,ta,len; the for(intI=1; i<=m; i++) { -Cin>>he>>ta>>Len; -maps[ta][he]=maps[he][ta]=Len; - } + intx, y; -Cin>>x>>y; + for(intK =1; K <= N; k++) A for(inti =1; I <= N; i++) at for(intj =1; J <= N; J + +) { - if(maps[i][j]>maps[i][k]+Maps[k][j]) -MAPS[I][J]=MAPS[I][K]+MAPS[K][J];//to update - } - -printf"%d", Maps[x][y]); in}
floyed algorithm O (N3) x