Although the author in the "algorithm-graph theory" column has discussed the shortest path problem, but here is still a re-discussion, Confucius also said, Wen know new.
The so-called shortest path problem, is based on a graph g<v, e>, the graph of the edge set E is weighted, and then discusses the search for a link between two points of the path, so that the path is all connected to the path of the least edge.
Find the shortest path--floyd-warshall algorithm between any two points.
The algorithm, which was developed in 1978 by Robert W.floyd and Stephen Warshall, who later transferred to the computer and was awarded the Turing Award in the year 1962, is a combination of two names to commemorate them.
In fact, the idea of this algorithm is very simple, we currently want to find the shortest path in G VI to VJ, the simplest method of violence is to locate the VI to VJ all the paths to go, and then maintain a minimum value. But now the question is how to find all the paths? We find a middle variable k,vi->vj path can be decomposed into VI->VK, vk->vj path, and K traversal [I,J] can enumerate all cases, you may ask that VI->VK still have a lot of paths, that will not continue to find? In fact, we set E[I][J] to record the shortest path of VI->VJ, and VI->VK, VK->VJ is also based on E[i][k], e[k][j], so we actually enumerate a series of relatively optimal cases, Then we find the current optimal solutions in these relative optimal solutions, and this is the place that the dynamic programming idea embodies.
Essentially, the algorithm is a clever combination of enumeration and dynamic programming.
#include <cstdio>using namespacestd;intMain () {inte[Ten][Ten], K, I, J, N, M, t1,t2,t3; intINF =9999999; scanf ("%d%d",&n,&m); for(i =1; i<=n;i++) for(j =1; j<=n;j++) if(i = = j) E[i][j] =0; ElseE[I][J] =inf; for(i =1; i<=m;i++) {scanf ("%d%d%d",&t1,&t2,&T3); E[T1][T2]=T3; } for(k=1; k<=n;k++) for(i=1; i<=n;i++) for(j=1; j<=n;j++) if(E[i][j] > E[i][k] + e[k][j])//the core part of the Floyd-warshall algorithmE[I][J] = E[i][k] +E[k][j]; for(i=1; i<=n;i++) { for(j =1; j<=n;j++) printf ("%10d", E[i][j]); printf ("\ n"); } return 0;}
"Aha algorithm"--Shortest path