Extension of the Floyd algorithm
The Floyd algorithm is a shortest distance from any point to arbitrary point. It is possible to find the shortest path of a negative value, a direction graph, a non-direction graph, etc. But the edge of the right can have negative weight of the side, but can not contain the negative weight of the side of the loop, otherwise the calculation is not the correct answer!! This algorithm is very inefficient and can be said to be violent. But this violence is violent with dynamic planning!! But there is no right side or negative loop.
On the Code
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineINF 1<<29intmp[ -][ -];intn,m;intMain () { while(~SCANF ("%d%d", &n,&m)) {//represents a weighted edge of N-node M-Bar for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++){ if(I!=J) {//initializing the default distance represents the distance from the I node to the J node. nodes with the same distance of 0mp[i][j]=Inf; }Else{Mp[i][j]=0; } } } intA,b,c; for(intI=1; i<=m;i++) {scanf ("%d%d%d",&a,&b,&c); MP[A][B]=C; //mp[b][a]=c;//This is a no-show diagram. } for(intk=1; k<=n;k++) {//The main code implementation area, you can simulate a bit. The key is to simulate, and then think for yourself why. for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++){ if(i!=j&&j!=k) {Mp[i][j]=min (mp[i][j],mp[i][k]+Mp[k][j]); } } } } for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++) {//The distance from any point to any point is output hereprintf"dis%d%d =%d\n", I,j,mp[i][j]); } } } return 0;}
Extension of the Floyd algorithm