The shortest path between any two points of the shortest floyd

 

It is limited, at the problem is not the solution, a lot of understanding

 

Thank you for watching this konjac

 

 

floyd algorithm:

Set D [k, i, j] represents the "number via a plurality of node k does not exceed the"  shortest path length from i to j

D [k, i, j] = min (D [k-1, i, j], D [k-1, i, k] + D [k-1, k, j]);

Initially D [0, i, j] = A [i, j]; A is the adjacency matrix

With = (V, E), V is a set of points, E is the set of edges, (x, y) represents a directed graph G from x to y directed graph, which edge weights (or length) of W (x , y). Provided n = | V |, m = | E |, the adjacency matrix A is a matrix of n * n.

A is defined as follows:

A[i,j]={  0   i=j

     w (i, j) (i, j) belonging to the E

     + ∞ (i, j) does not belong to E

    }

Therefore, k is a phase, it must be placed in the outermost loop

K DP omitted after the dimension

D [i, j] = min (D [i, k] + D [k, j]);

The final D [i, j] for the i to j path length

 

 Templates are as follows:

 

code:

 1 #include<bits/stdc++.h>
 2 #pragma GCC optimize(3)
 3 
 4 using namespace std;
 5 int n,m;
 6 int f[310][310];
 7 inline int read(){
 8     int x=0,f=1;char ch=getchar();
 9     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
10     while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
11     return x*f;
12 }
13 inline void write(int x)
14 {
15     if(x<0)x=-x,putchar('-');
16     if(x>9)write(x/10);
17     putchar(x%10+'0');
18 }
19 int main()
20 is  {
 21 is      Memset (F, 0x3F , the sizeof (F)); // initial maximum distance 
22 is      for ( int I = . 1 ; I <= n-; I ++) F [I] [I] = 0 ; // themselves to their distance of 0 
23 is      n-= Read (), m = Read ();
 24      for ( int I = . 1 , X, Y, Z; I <= m; I ++ ) {
 25          X = Read (), Y = Read ( ), Z = Read ();
 26 is          F [X] [Y] = min (F [X] [Y], Z); // build adjacency matrix 
27      }
 28      for ( int K =1;k<=n;k++){
29         for(int i=1;i<=n;i++){
30             for(int j=1;j<=n;j++){
31                 f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
32             }
33         }
34     }
35     return 0;
36 }

application:

Transitive Closure

 

code:

 1 #include<bits/stdc++.h>
 2 #pragma GCC optimize(3)
 3 
 4 using namespace std;
 5 int n,m;
 6 bool f[310][310];
 7 inline int read(){
 8     int x=0,f=1;char ch=getchar();
 9     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
10     while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
11     return x*f;
12 }
13 inline void write(int x)
14 {
15     if(x<0)x=-x,putchar('-');
16     if(x>9)write(x/10);
17     putchar(x%10+'0');
18 }
19 int main()
20 {
21     for(int i=1;i<=n;i++)f[i][i]=1; 
22     n=read(),m=read();
23     for(int i=1,x,y,z;i<=m;i++){
24         x=read(),y=read(),z=read();
25         f[x][y]=f[y][x]=1;
26     }
27     for(int k=1;k<=n;k++){
28         for(int i=1;i<=n;i++){
29             for(int j=1;j<=n;j++){
30                 f[i][j]|=f[i][k]&f[k][j];
31             }
32         }
33     }
34     return 0;
35 }

 

Guess you like

Origin www.cnblogs.com/nlyzl/p/11770430.html