Floyd algorithm for finding the shortest distance of each vertex - Algorithm Analysis and Practice job 2-1

1. Problem

Floyd algorithm with the shortest distance of each vertex for Solving FIG. Write algorithm and Floyd pseudocode given distance matrix (matrix shortest distance between vertices)

2. Parse

There are four eight figure above urban highway, the numbers on the road indicate the length of this road. Please note that these roads are one-way. We now request the shortest distance between any two cities, which is the shortest path between any two points. This problem is also known as "multi-source shortest path" problem.

Now you need a map to store information data structure, we can still use a matrix (two-dimensional array e) a 4 * 4 to store. Example No. 1 to No. 2 urban city run is 2, then let E [1] [2] a value of 2.2 city No. 4 does not reach the city, the set E [2] [4] value of ∞. Also here a convention to their own city is also 0, for example, e [1] [1] is 0.

Let's think about, according to our experience, if you want the distance between any two points (e.g., a point from the vertex to the vertex b) becomes shorter, only the introduction of a third point (vertex K), and by the k vertices transit i.e. a-> k-> b, it is possible to shorten the original point from the vertex to the vertex b of a distance. Then the transit vertex k 1 ~ n which point in it? And sometimes by more than one point, but after two or more points of transfer points is shorter, i.e., a-> k1-> k2b-> or a-> k1-> k2 ... -> k-> i ... - > b. From the figure above, such as 3 to 4 urban city (4-> 3) away E [4] [3] was originally 12. If only urban transit through a No. 1 (4-> 1-> 3), the distance is shortened to 11 (e [4] [1] + e [1] [3] = 5 + 6 = 11). In fact, No. 1 city to 3 city may be through a No. 2 urban transit, so that No. 1 to No. 3 from the city shortened to 5 (e [1] [2] + e [2] [3] = 2 + 3 = 5 ). Therefore, if both 1 and 2 through the two city transit, then from 3 to 4 cities from the city further reduced to 10. By this example, we find that the distance of each vertex are possible between two other vertices becomes shorter.

3. Design

1.	#include
2.	int main()
3.	{
4.	    int e[10][10],k,i,j,n,m,t1,t2,t3;
5.	    int inf=99999999; //用inf(infinity的缩写)存储一个我们认为的正无穷值
6.	 
7.	    //读入n和m,n表示顶点个数,m表示边的条数
8.	    scanf("%d %d",&n,&m);
9.	 
10.	    //初始化
11.	    for(i=1;i<=n;i++)
12.	         for(j=1;j<=n;j++)
13.	             if(i==j) 
14.	                e[i][j]=0;
15.	             else 
16.	                e[i][j]=inf;
17.	 
18.	     //读入边
19.	     for(i=1;i<=m;i++)
20.	     {
21.	         scanf("%d %d %d",&t1,&t2,&t3);
22.	         e[t1][t2]=t3;
23.	     }
24.	 
25.	     //Floyd-Warshall算法核心语句
26.	     for(k=1;k<=n;k++)
27.	         for(i=1;i<=n;i++)
28.	             for(j=1;j<=n;j++)
29.	                 if(e[i][j]>e[i][k]+e[k][j] )
30.	                     e[i][j]=e[i][k]+e[k][j];
31.	 
32.	    //输出最终的结果
33.	     for(i=1;i<=n;i++)
34.	     {
35.	        for(j=1;j<=n;j++)
36.	         {
37.	             printf("%10d",e[i][j]);
38.	         }
39.	     printf("\n");
40.	     }
41.	 
42.	 return 0;
43.	}

4. The source address

https://github.com/Lin02993/-

Released three original articles · won praise 0 · Views 18

Guess you like

Origin blog.csdn.net/weixin_43246332/article/details/104762179