Floyd
In my opinion this is the simplest of the shortest path algorithm, and also the low one.
So a big guy in our group gave him a new name, called Timeout!!!
(In fact, if the data range is very small, this algorithm is very good use!!) )
This algorithm is relatively simple, or we directly on the code?!
#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#defineMAXN 1000+15#defineOO 123456using namespacestd;intDIS[MAXN][MAXN];intMain () {intx,y,z,n,m; scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) Dis[i][j]=oo* (i!=j);//In addition to the point to the rest of the points outside of himself, the rest of the points are all paid to the maximum value for(intI=1; i<=m;i++) {scanf ("%d%d%d",&x,&y,&z); Dis[x][y]=z;//Input two inter-city and distance} for(intk=1; k<=n;k++)//Enumerate intermediate points for(intI=1; i<=n;i++)//enumeration starting point for(intj=1; j<=n;j++)//Enumerate EndpointsDis[i][j]=min (dis[i][j],dis[i][k]+Dis[k][j]); printf ("%d", dis[1][n]);//outputs the shortest path from 1 to n return 0;}
It is also important to note that the Floyd-warshall algorithm cannot solve a graph with a "negative weight loop" (or "negative weight loop") because there is no shortest path with a "negative weight loop" diagram.
Floyd algorithm of shortest circuit