Shortest Floyd ()

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N = ???;
 5 const int INF = 0x3f3f3f3f;
 6 int n, m;
 7 int a, b, x;
 8 int dis[N][N];
 9 
10 void Floyd(){
11     for(int k=0; k<n; k++)
12         for(int i=0; i<n; i++)
13              for( Int j = 0 ; j <n-; j ++ )
 14               // selected from i to j shortest (known comparison current i to j with the shortest distance and then the distance from i to j K) 
15                   DIS [i] [J] = min (DIS [I] [J], DIS [I] [K] + DIS [K] [J]);
 16  }
 . 17  
18 is  int main ()
 . 19  {
 20 is      the while (Scanf ( " % D% D " !, & n-, & m) = the EOF) {
 21 is          // initialization 
22 is          for ( int I = 0 ; I <n-; I ++ ) {
 23 is              for ( int J = 0 ; J <n-; J ++)
 24                  DIS [I] [J] = INF;
 25              DIS [I] [J] = 0 ;
 26 is          }
 27          // input points a distance 
28          for ( int I = 0 ; I <m; I ++ ) {
 29              Scanf ( " % D% D% D " , & A, & B, & X);
 30              X = min (DIS [A] [B], X);    
 31 is              DIS [A] [B] = DIS [B] [A] = X;
 32          }
 33 is          //  
34 is          the Floyd ();
 35          //  
36         int S,T;
37         scanf("%d %d",&S,&T);
38         if(dis[S][T]==INF)    printf("-1\n");
39         else                printf("%d\n",dis[S][T]);
40     }
41     
42 }

Guess you like

Origin www.cnblogs.com/0424lrn/p/12237605.html