Floyd's shortest path algorithm (brute force exhaustion)

topic description

In the weighted directed graph G, finding the shortest path problem between any pair of vertices in G is also a very common problem.
One way to solve this problem is to perform Dijkstra's algorithm n times, so that the shortest path between each pair of vertices can be found, and the time complexity of execution is O(n3).
Another algorithm was proposed by Freud, the time complexity is also O(n3), but the form of the algorithm is much simpler.
In this question, read in a weighted adjacency matrix (that is, an array representation) of a directed graph, build a directed graph and find the shortest path length between each pair of vertices according to the algorithm described above.

enter

The first line of input contains a positive integer n, indicating that there are n vertices in the graph. where n does not exceed 50.
Each of the next n lines has n integers separated by spaces. For the j-th integer in the i-th line, if it is greater than 0, it means that the i-th vertex has a directed edge pointing to the j-th vertex, and the weight is the corresponding integer value; if this integer is 0, it means that there is no i A directed edge pointing to j. When i and j are equal, the corresponding integer is guaranteed to be 0.

output

There are n lines in total, and each line has n integers, indicating the shortest path length from the source point to each vertex. If there is no path from the source to the corresponding vertex, output -1. For the length of the shortest path from a vertex to itself, output 0.
Please output a space after each integer, and note the newline output at the end of the line.
#include <iostream>
#include <iomanip>
using namespace std;
  
#define Max 1000
#define MVNum 100         //最大顶点数
#define OK 1
  
typedef int VerTexType; //顶点信息
typedef int OtherInfo;    //和边相关的信息
typedef int ArcType;
  
//- - - - -图的邻接表存储表示- - - - -
typedef struct {
   VerTexType vexs[MVNum];            //顶点表
   ArcType arcs[MVNum][MVNum];      //邻接矩阵
   int vexnum, arcnum;                //图的当前点数和边数
} Graph;
  
void CreateUDG(Graph &g)
{
   //采用邻接矩阵表示法,创建无向图G
   /****在此下面完成代码***************/
    int i,j;
    cin>>g.vexnum;
    for(i=0;i<g.vexnum;i++)
    {
        for(j=0;j<g.vexnum;j++)
        {
            cin>>g.arcs[i][j];
            if(i!=j&&g.arcs[i][j]==0)g.arcs[i][j]=Max;
        }
    }
   /***********************************/
}//CreateUDN
  
  
void shortFloyd(Graph g)
{
    int i,j,k,n=g.vexnum;
    int d[n][n],p[n][n];
    for(i=0;i<g.vexnum;i++)
    for(j=0;j<g.vexnum;j++)
    {
        d[i][j]=g.arcs[i][j];
        if(d[i][j]<Max&&i!=j)p[i][j]=i;
        else p[i][j]=-1;
    }
    for(k=0;k<g.vexnum;k++)
    for(i=0;i<g.vexnum;i++)
    for(j=0;j<g.vexnum;j++)
    {
        if(d[i][k]+d[k][j]<d[i][j])
        {
            d[i][j]=d[i][k]+d[k][j];
            p[i][j]=p[k][j];
        }
     } 
    for(i=0;i<g.vexnum;i++)
    {
        for(j=0;j<g.vexnum;j++)
        {
            if(i!=j&&d[i][j]==Max)cout<<-1<<" ";
            else cout<<d[i][j]<<" ";
        }
        cout<<endl;
    }
}
  
int main()
{
   Graph g;
   CreateUDG(g);
   shortFloyd(g);
   return 0;
}//main

Sample input Copy

4
0 3 0 1
0 0 4 0
2 0 0 0
0 0 1 0

Sample output Copy

0 3 2 1 
6 0 4 7 
2 5 0 3 
3 6 1 0 

hint

In this question, it is necessary to complete the Floyd algorithm according to the algorithm in the title description, and record whether each vertex is reachable during the calculation of the shortest path. able to end.
Compared with Dijkstra's algorithm, the form of Floyd's algorithm is simpler. Through a triple loop, Floyd's algorithm can easily find the shortest distance between each pair of vertices.
In addition, it should be noted that in order to more conveniently represent the unreachable state between vertices, a very large value can be used as a flag. The algorithm example in the topic description uses another three-dimensional array to represent it, which increases the original O(n3) time complexity to O(n4), which is also the part that needs to be modified by itself.

Guess you like

Origin blog.csdn.net/qq_63306482/article/details/124867717