Detailed Floyd

Summer vacation, a small hum ready to go to some city tours. There are some among urban highway, it is not between some cities, as shown below. In order to save money and convenient trip planning, small hum wanted to know the shortest distance to any city in the previous two before departure.
 
 
        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, as follows.
        Now back to the question: How to find the shortest path between any two points of it? By learning before we know the depth or breadth-first search can find the shortest path between two points. So be n2 times the depth or breadth-first search, that is, for every two points once the depth or breadth-first search, you can obtain the shortest path between any two points. But there is no other way to do that?
 
        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. Well, let's generalize this problem.
 
        When allowed to pass the third point between any two points, the shortest distance between these cities is the initial journey, as follows.
       
 
 
If we were only allowed to go through vertex No. 1, find the shortest distance between any two points, how to find it? Analyzing only e [i] [1] + e [1] [j] to whether the ratio of e [i] [j] is smaller. E [i] [j] indicates the number i from vertex to vertex distance between the j-th. e [i] [1] + e [1] [j] indicates the number of vertices from first to i vertices No. 1, No. 1 and from vertex to vertex distance and the number j. Where i is cycle 1 ~ n, j is 1 ~ n cycles, the following code is implemented.

 

1 for (i = 1; i <= n; i++)
2             {
3                 for (j = 1; j <= n; j++)
4                 {
5                     if (e[i][j] > e[i][1] + e[1][j])
6                         e[i][j] = e[i][1] + e[1][j];
7                 }
8             }

 

但是事实上我们不仅仅只允许经过1号顶点,求任意两点之间的最短路径。

简而言之就是:从i号顶点到j号顶点只经过前k号点的最短路程。

 

那么关于k的这个循环我们到底是放在最外面还是放在里面呢?这个就需要我们对FLyod算法有更深一步的认识。这个算法实际上是个动态规划算法

 

 

正因为如此所以我们的 k 自然要放在最外面了!

 

核心代码:

1 for(k=1;k<=n;k++)  
2     for(i=1;i<=n;i++)  
3     for(j=1;j<=n;j++)  
4     if(e[i][j]>e[i][k]+e[k][j] )   
5                         e[i][j]=e[i][k]+e[k][j];  

 

 

整体的代码:

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

 

Guess you like

Origin www.cnblogs.com/-Ackerman/p/11237114.html